I was looking through my code to try and fix a problem, then I came across what was causing it, but I am unable to fix it. As below, I set Move1 to null earlier in the script, and the variable does not change anywhere else in the script other than the two places I am showing you.
private GameObject Move1 = null;
The problem is that when I do Debug.Log(Move)
the first time, it does not return as "null". However, when I call Debug.Log(Move)
for the second time, it does return as null and I am unsure why.
private void addVar(GameObject Move, GameObject Cyl)
{
Debug.Log(Move);
if (Move1 == null)
{
Move = Move1;
Cyl = Cyl1;
Moves.Add(Move);
Movecyls.Add(Cyl);
Debug.Log(Move);
}
I have already tried doing private GameObject Move1;
but that doesn't work. That's about all the ideas I have. Thanks!
because you are assigning Move = Move1;
only when Move1 == null
=> Move = null;
Note in general: You shouldn't use == null
on Unity built-in types inheriting from Object
at all.
Rather use the implicit operator bool
like eg
if(!Move1)
{
...
The reason is that internally Object
might not be set to a valid reference but will still hold some information about why it is returning a value that equals null
. That's the reason why you don't get the usual NullReferenceException
for Unity types but eg MissingReferenceException
. It is not really null
though so a check == null
might fail in some occasions.
May it be, that you are mixing up Move = Move1;
and Move1 = Move;
?
Your code as it stands with some comments on what is happening.
private void addVar(GameObject Move, GameObject Cyl)
{
// The function parameter is whatever sent to this function
// It may or may not be null
Debug.Log(Move);
if (Move1 == null)
{
// The following will always result in null
// Because of the above if condition
Move = Move1;
Cyl = Cyl1;
Moves.Add(Move);
Movecyls.Add(Cyl);
// Debug value will now be null
Debug.Log(Move);
}
// Other operations
}
You're logging "Move" at the beginning but it is not defined as null, therefore it wont log as null. The second debug you set it to null since "Move1" is null, that's why you get null the second time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.