- Home /
Active game object before destroying another
Hey guys, I'm using JavaScript for this one. In my script I am using
Destroy (gameObject);
But before the script destroys the objects that it is on, I wanted it to enable another gameobject in the scene.
I can post the script I am using if you like, but it seems pointless? Just know the script I\m using destroys itself, I am wondering where and what I would add to enable another game object in the scene before it destroys. Any ideas? :) hope that wasn't worded weirdly lol
Answer by jmparavicini · Feb 22, 2014 at 10:31 PM
Im not very sure but that can maybe help var (yourgameobject) : GameObject;
function Update ()
{
if(input.GetKeyDown(Keycode.Space))
{
(yourgameobject).active = true;
Destroy(gameObject);
}
}
Answer by robertbu · Feb 22, 2014 at 09:39 PM
If it is destroying itself, why not the line before:
GameObject.Find("SomeOtherSceneObject").SetActive(true);
Destroy(gameObject);
Alternately you can use the OnDestory() callback:
https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnDestroy.html
I applied that part of script to my script and it said, object reference not set. Where should this be applied? I keep getting the same error when I tried sometihng similar myself, I must be doing something stupid lol
Edit: "someotherscene object" is that the name of another object in the scene just as it says? or should that be a tag or sorts? The object I want activated is off by its lonesome
Edit: Just now I applied the code, and I change it from true to False. And it turned off the object ins$$anonymous$$d, that worked. SO I think the issue is, the object still destroys itself before it can make the gameobject true.
'SomeOtherSceneObject' is the name of the object that is 'by its lonesome'. You could do it by tag using GameObject.FindWithTag(). Put a Debug.Log() statement between these two lines.
GameObject.Find("SomeOtherSceneObject").SetActive(true);
Debug.Log("Got to here");
Destroy(gameObject);
If the 'Got to here' fires, then the object should have been activated. Note the 'Find()' find the first object in the scene with that name. If the object you are about to delete has the same name, it may be finding the current object ins$$anonymous$$d. You can get around that problem (if that is the issue) by changing the name of the current game object.
What is interesting is, if I set it to (false), it will make it to the dedug log. But when true it says no reference
Oh, stupid me. GameObject.Find() only finds active objects. So this line:
GameObject.Find("devil_flame").SetActive(true);
...will be producing a null reference exception if the object is disabled. Here are some choices:
Create a public GameObject variable reference in the top of this file and initialize it by dragging and dropping 'devil_flame' onto the variable. Then you can use this variable to activate the object.
Start with 'devil_flame' active. In Start() use GameObject.Find() to find the object, save the reference then disable it.
Don't disable 'devil_flame'. Hide it ins$$anonymous$$d by turning off the render and any other components that need to be disabled. This script would then enable those components.
And there are a few other more obscure solutions.
If both objects exist in the scene at start, the first choice is easiest. At the top of the file put:
var devilFlame : GameObject;
Initialize this variable by dragging the 'devil_flame' game object onto this variable. In the code you can enable it by:
devilFlame.SetActive(true);
Thank you good sir! Your answer had been given thumbs up lol, if it doesn't show let me know, it tends to do that time to time!
Your answer
