- Home /
How to access a variable from another script?
Hello, I am new to Unity and programming in general. I am getting the exception:
"NullReferenceException: Object reference not set to an instance of an object ammoCount.Update () (at Assets/Standard Assets/scripts/ammoCount.js:6)"
when trying to access a variable from another script. In my case the scripts are "shoot" and "ammoCount". Shoot contains the variables ammo and mag which I want to access from ammoCount and display. I have already tried this code in ammoCount script:
var ts : shoot;
function Update () {
var mag = ts.mag;
var ammo = ts.ammo;
guiText.text = mag + "/" + ammo;
}
Any help would be appreciated, Thanks
Hey, type the title of your question into the search bar above.
I'm not being funny, really this is asked daily, you'll get 100's possibly 1000's of results.
I guess from the lilac color means you can't get an answer as that's been blocked out but your title is so well made it makes a perfect search.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183320im_/https://answers.unity.com/themes/thub/images/avi.jpg)