if an object gets destroyed and it was set as the value of variables in otehr scripts what do the variables return as. are they null or something else
The shared variables in other scripts should not be affected. Only once all references to an object are lost (i.e., the other scripts are destroyed as well) are those variable values removed from memory.
(at least, that’s how it should be. Unity may do other work behind the scenes that invalidates this, but this behaviour I described is pretty standard in object oriented programming) Probably the best advice I can give is to create a simple test-case scenario with two simple scripts/objects and just try what you’re planning to do. If it works, fantastic! If it doesn’t, work around it.
i need to know how to detect that the game object set to the variable is destroyed
are they null before they are set? if so how can i detect they are removed and then set them back to whatever they are when they are no9t set.
public GameObject myVar;
void Update()
{
if (myVar) // You can also use myVar != null
{
...
}
}
in JS:
public var myVar : GameObject;
function Update()
{
if (myVar) // You can also use myVar != null
{
...
}
}
You mean you have something like this (pseudo-code):
class Foo
{
var myBar:Bar;
function Start()
{
myBar = new Bar();
myBar.Target = new GameObject();
}
function DestroyTarget()
{
Destroy(myBar);
}
function CheckDestroyed()
{
if (myBar.Target == null)
{
//destroyed
}
}
}
class Bar
{
var Target:GameObject;
}
Where on CheckDestroyed you can tell if that target was removed by some other script?
FizixMan, when you call CheckDestroyed(), myBar is null at that point, so trying to figure out if target is null, will throws an exception because myBar doesn’t exist.
Your code must be like this:
function CheckDestroyed()
{
//here you are checking if gameObject was removed.
if (myBar == null)
{
//myBar was destroyed!
}
}
}
My code won’t throw a null reference exception unless Unity does something special with removing objects from the scene, myBar should still be non-null unless you explicitly assign it as such. I’m assuming this is the observed behaviour as it is standard garbage collection behaviour (see my post above).
If you explicitly assign it to null, then you’re going to have to find a different way to track the various properties that were assigned to myBar.
EDIT: maybe you can give us a better idea of what you’re trying to do. Sounds like you’re making this harder than it needs to be.
You are right FizixMan, myBar could keep its value if the garbage collector didn’t clean the variable.
Well, drazil austin… it’s time to talk!