- Home /
How to access variables from a different script on a different GameObject?
I have a game object Called PistolAmmo with a script called AmmoPistol.js attached to it, i also have the player game object with a script called WeaponManagerScript.js(this script manages the ammo count for all weapons), i want AmmoPistol.js to be able to access and modify variables inside of WeaponManagerScrit.js. I have been trying for the past eighteen hours to get this to work but the closet i have gotten is this.
var weaponManager : WeaponManagerScript;
function Start ()
{
weaponManager = gameObject.GetComponent("WeaponManagerScrip");
Debug.Log ("Hello");
}
function OnTriggerEnter (collider : Collider)
{
Debug.Log ("Something is flipping my trigger!!!!");
if(collider.gameObject.FindWithTag("Player"))
{
Debug.Log ("Oh babay you Just Flipped My Trigger!!!");
if (weaponManager.ammoCountPistol <= 48)
{
Debug.Log ("Its Dangerous Ahead You'll Need This!");
weaponManager.ammoCountPistol += 8;
Destroy(this.gameObject);
}
}
}
The above code gives me this error.
NullReferenceException: Object reference not set to an instance of an object
PistolAmmo.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/WeaponScripts/PistolAmmo.js:17)
If anyone knows a way to do this or fix any current problems in my script i would appreciate the help, thanks.
Answer by mStep · Jan 31, 2013 at 10:51 PM
This line:
weaponManager = gameObject.GetComponent("WeaponManagerScrip");
is spelled wrong, I believe. Unless your script is actually called "WeaponManagerScrip". But more importantly, you haven't referenced the gameObject that WeaponManagerScript.js is attached to. You've just used the generic "gameObject", which means you are trying to get the instance of WeaponManagerScript.js that is attached to the PistolAmmo object. If you wanted to get the instance of WeaponManagerScript.js that is on a different gameObject, you would need to either reference it directly through a public variable, or find it, like this:
weaponManager = GameObject.Find ("TheObjectWIthWeapnManager").GetComponent(WeaponManagerScript);
You also need to make sure the variable you're trying to access is public, and not protected.
Sweet mother of Iverseen, I have been staring at my monitor for sooooo long trying to fix this my eyes have turned completely red, and the fix was so darn simple. Thank you so much, if i could up vote you, i would.
Not a problem. Sometimes the simplest answers are the hardest to find.
Answer by nickseegobin · Jan 31, 2013 at 11:52 PM
STATIC VARS
Set the variables as "static";
firstScript.js:
public static var score:int = 2;
secondScript.js:
//prints out the value in the console
Debug.Log(firstScript.score);
Your answer
![](https://koobas.hobune.stream/wayback/20220613091935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting errors when referencing a variable. 1 Answer
Public variable in script different for every game object. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How can I set the order of destruction of game objects? 1 Answer
How to save a gameObjects on a prefab and variables with a script? 1 Answer