- Home /
what the heck do you put in the object reference!? all i need is a variable!
many people have asked questions about this but NONE answer my very simple question properly.
i simplified it down as far as i could go. i am simply trying to debug.log the value of a variable from an object by clicking on another object.
public class test : MonoBehaviour {
public int numToBeAccessed;
private void Start()
{
numToBeAccessed = 10;
}
}
and the other script:
public class test2 : MonoBehaviour {
public test numAccessor;
public void OnMouseDown()
{
numAccessor = GetComponent<test>();
Debug.Log(numAccessor.numToBeAccessed);
}
}
what the heck do i put in the object reference numAccessor?!
im just trying to debug.log a variable from another script.
the error reads at the debug.log command saying:
NullReferenceException: Object reference not set to an instance of an object test2.OnMouseDown () (at Assets/scripts/test2.cs:12) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
Answer by TheSOULDev · Aug 20, 2017 at 02:47 AM
How can Unity tell which script you're looking to debug it from? If you want to debug it without a reference, make the numToBeAccessed
static and call it as test.numToBeAccessed
. Otherwise you'll need to add your script to the same object that holds the script you're trying to access it from.
If you still don't understand why you need to do this, I suggest you read C# manuals and learn what OOP is, otherwise you'll have a hard time adapting to Unity.
well i did that and it worked so thank you i wish i could hug you i was about to give up.
im very new and dont understand why in the dozens of videos and posts i read when searching for "how to access a variable from another script" did none of them just say "oh ya just make it static" you are a legend sir thank you again.
Well, the thing with static variables is that you can only use them for one thing at a time. If you have a script that controls different objects (let's say a movement script that lets different objects move how they want), then making something static won't work the way you want it to. $$anonymous$$aking variables static works only if that variable is used by all users equally and if you only need one instance of it. For everything else, you will need to point to a script you want to take the value from, so if you have 2 or more of the same scripts running on different object, Unity knows which one to look at (they're all different despite being the same class).
Answer by realcyberleon · Jul 02, 2021 at 08:31 AM
Holy mother of god @TheSOULDev ,
I went - headbanging my head on my keyboard - through half of THIS https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it until I found your solution to my very problem, for I was doing exactly the same thing as @captainspaceman , namely debugging by following a variable from one script through another.
THANKS!
P.S., If you could give us a good (small one-or-few-more-liner-code) hint on how to do what you describe
that would be much appreciated, at least by the two of us!Blockquote "... you will need to point to a script you want to take the value from, so if you have 2 or more of the same scripts running on different object, Unity knows which one to look at (they're all different despite being the same class)."
Your answer
