6

I am doing a school project. I need to check the destroyed objects in my scene as soon as the scene starts. problem is I don't know how to make it load or where to attach the c# script.

public static class DestroyedObject { static List<GameObject> objs = new List<GameObject>(); public static void Add(GameObject obj) { if(!objs.Contains(obj)) objs.Add(obj); } } 
3
  • 2
    You have some destroyed objects when scene starts? Why creating and destroying something before scene starts? Commented Jan 9, 2017 at 13:35
  • its a hidden object game, the found objects are destroyed then you move to another scene, you can comeback to the previous scene thats why there are objects that needs to stay destroyed Commented Jan 9, 2017 at 13:37
  • Didn't even noticed you strictly copy-pasted the code @m-rogalski provided you in your previous question : maybe you could simply have asked him instead of starting a new question... Commented Jan 10, 2017 at 9:53

3 Answers 3

8

If you want it to run when you start the scene you need to attach it to a GameObject. Create empty and attach it as a component.

The code that you want to run on start should be in the:

void Awake { //Your code here } 

or

void Start { //Your code here } 

functions.

Start is called as soon as the class is instantiated and Awake is called when the scene is started. Depends where you want it in the call stack, but in your case i think it will do essentially the same thing.

Sign up to request clarification or add additional context in comments.

6 Comments

Not sure if your answer was clear enough about this point so I prefer emphasizing it here : all Awake() methods from the scene will always be called before any Start() method. Awake() is rather the moment the class is instantiated (happens if script is disabled on an active GameObject) while Start() is called only the first time Unity "sees" the script being enabled on an active GameObject. You can check this for a better understanding of Unity execution order.
How will i make it run without clicking anything?
If you place the GameObject with the script attached in the scene and run the scene, all code that you have written in the Start()method will run.
@JrRosales Put it on an empty GameObject, which doesn't render
Correct call order is Awake then OnEnable and then Start
|
1

I think what you're looking for is a way to "save" what objects have been deleted : you simply have to make your class inherit from MonoBehaviour and call DontDestroyOnLoad() so your object containing the script will exist between the scenes.

public static class DestroyedObject : MonoBehaviour { public static DestroyedObject Instance; private static List<GameObject> objs = new List<GameObject>(); private void Awake() { if (!Instance) { Instance = this; } else { DestroyImmediate(gameObject); } DontDestroyOnLoad(gameObject); } public static void Add(GameObject obj) { if(!objs.Contains(obj)) objs.Add(obj); } public static List<GameObject> GetDestroyedObjects() { return objs; } } 

Then you simply access your script using DestroyedObject.Instance.Add() or DestroyedObject.Instance.GetDestroyedObjects() (some people don't like this kind of design pattern but it has proven to be very effective when using Unity).

Also as @Sergey asked, why creating objects (on scene loading) in order to delete them afterward : you could do the revers operation (only instantiate the needed ones).

Hope this helps,

Comments

0

Can you describe what you are trying to achieve in total? Because it looks like your way is not the best way to do it ;).

If all you want to know is how to execute a script at scene start: create a script that inherits from MonoBehaviour (no need for static class), attach it to a gameobject in your scene, and thats it!

If you want to execute code as soon as the scene starts (and the gameobject is loaded), put your code in Awake() or Start(). You can read about the execution order of those functions here: https://docs.unity3d.com/Manual/ExecutionOrder.html

Making a script static means it will be active in all scenes and even before any scene is loaded.

Additionally, i would not recommend the use of static classes unless you really need them.

1 Comment

Yeah, i would have commented but i can't -_-. Nevertheless, what Alex said about Start and Awake is not completely true and i don't think using a static class is the best solution in this case either (because its avoidable).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.