- Home /
How do i access a variable from another script?
Hi. I want to check an int called buff, from another script (Player script). What i want is: when the buff=1, i want it to change my buffOn int to 1, which is placed in a different script than the player script. Right now i get this erroe "NullReferenceException: Object reference not set to an instance of an object ToughBox.Update () (at Assets/ToughBox.cs:17)"
My script looks like this:
using UnityEngine; using System.Collections;
public class ToughBox : MonoBehaviour { public int buffOn;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Player Pscript = GetComponent<Player>();
if (Pscript.buff == 1)
{
buffOn = 1;
}
}
the object that this script is on, does it have the Player script attached too? Also it says the error is on line 17, but you have only supplied up to line 13.
The object this script is on, does NOT have the Player script attached to it. Don't know why it displays the lines like this, but in my script line 17 is line 9 (if (Pscript.buff == 1))
Answer by akauper · Apr 13, 2014 at 08:36 PM
The error is letting you know that Pscript is not correctly referenced to your instanced script. In other words, your GetComponent() method is not working.
For GetComponent to work both scripts must be attached to the same game object. Is this the case?
Also, there is rarely a need to call GetComponent from within Update (it slows things down). Assign your Pscript variable within Start or Awake instead.
No the Player script is not attached to the object which this script is on.
But if i set GetComponent in Awake will it notice if the int from the Player scrip changes?
Yes. What you are 'getting' is a reference to that script not a duplication of its current state. Any time you use dot notation to access the script it will access its current state.
But, more importantly, if both scripts are not on the same gameobject, then GetComponent wont work. First you need to find which gameobject the script is on, then use GetComponent.
For example,
gameObject.Find("nameofgameobject").GetComponent<Player>();
Simply using GetComponent() asks unity to find the player script attached to the same gameobject as the script which is calling GetComponent. GetComponent does not search for all scripts within any object in the scene.
Ok thanks for $$anonymous$$ching me that. However i still get an error. even though i find the Player gameobject and the Player script attached to it in Start ();
NullReferenceException: Object reference not set to an instance of an object ToughBox.Start () (at Assets/ToughBox.cs:14)
using UnityEngine; using System.Collections;
public class ToughBox : $$anonymous$$onoBehaviour { public int buffOn;
// Use this for initialization
void Start () {
GameObject.Find("Player").GetComponent<Player>();
Player Pscript = GetComponent<Player>();
Pscript.buff = buffOn;
}
(btw line 14 is the Pscript.buff = buffOn; part.)
// Use this for initialization
void Start () {
Player Pscript = GameObject.Find("Player").GetComponent<Player>();
Pscript.buff = buffOn;
}
Your first line under start is just declaring "find the gameobject and get the component on it... oh you have it? ok dont assign it to anything just load it into memory" then when your start function ends it is unloaded from memory.
The 2nd line then asks unity to Get the component Player on the current gameobject (on which it doesnt exist).
See my code above for the correct syntax
Answer by uanmanarmy · Apr 15, 2014 at 10:47 PM
public GameObject player; //attach the object with the script.
public NameofTheScript variable;
void Awake () {
variable = player.GetComponent<NameofTheScript> ();
}
// Update is called once per frame
void Update ()
{
variable.variablefromNameofTheScript;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613142631im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enable My Script on Spawn 0 Answers
Accessing variables from a different gameobject 1 Answer
How to get script from Object with (Clone)'s script? 2 Answers
how do you access booleans from another script 1 Answer
Why are these scripts getting different icons and could it have anything to do with my problem? 2 Answers