- Home /
Accessing variable from another class gives initial value
Hi, my problem is fairly simple: whenever I access a variable from another script, I only ever receive the value that I originally assigned to that variable. Here's my code:
public class PickupScript : MonoBehaviour {
public int progress = 0;
private CreatureNav script;
void Start () {
script = new CreatureNav ();
}
void Update () {
if(condition){
progress=1;
}
}
public int getProgress(){
return progress;
}
}
public class CreatureNav : MonoBehaviour {
private PickupScript script;
void Start () {
script = new PickupScript ();
}
public void Update () {
if (script.getProgress () == 1) {
RenderSettings.fog = true;
}
}
}
The most anoying bit is that I don't get any kind of error message, it just doesn't work! My intent is to create fog when "condition" is met, but nothing happens. I've done some bug checks, and "getProgress()" always returns 0. Accessing int "progress" directly using "script.progress" also turns up as 0. Can someone please explain what's going on?!
As far I know you can`t use --new-- word with $$anonymous$$onoBehaviour like in here
new PickupScript ();
The other thing is condition might be not met. Try
void Update () {
progress=1;
}
to test interace.
and
void Update () {
if(condition){
Debug.Log("condition met");
}
}
to test statement.
I have tested and confirmed that the condition works perfectly and is met when it is intended.
Your problem is probably that Unity doesn't call the methods on the PickupScript because it isn't assigned to a gameobject.
I have PickupScript attached to a gameobject as a script component.
Answer by CodeElemental · Apr 07, 2014 at 02:57 PM
The problem is that the script = new PickupScript (); Method instantiates a new instance of the script, unrelated to the one attached on the gameobject (which I assume is the one you want to use). So instead you need to change the code : from
void Start () {
script = new PickupScript ();
}
to
void Start () {
script = GetComponent<PickupScript> ();
}
But if it has to be created at runtime you can simply use AddComponent
Hmm.. what you're saying makes sense, but the console is reporting a NullReferenceExeption. Here's the new code: public class CreatureNav : $$anonymous$$onoBehaviour {
private PickupScript script;
void Start () {
script = GetComponent <PickupScript> ();
}
// Update is called once per frame
public void Update () {
if (script.progress == 1) {
RenderSettings.fog = true;
}
}
}
It refers to the first line of the "if" statement, and says, "Object reference not set to an instance of an object". In the other script, I have the line of code:
public int progress = 0;
So I think that it's talking about the script variable.
If it helps, my scripts are attached to two different objects. Can you help me again please?
Then you need to get reference to the game object the script is attached to, first :
void Start () {
GameObject go = GameObject.Find("Object name"); // Replace "Object name" with the name of your object.
script = go.GetComponent<PickupScript> ();
}
I'm still getting the NullReferenceExeption. The new start statement is:
void Start () {
GameObject go = GameObject.Find("$$anonymous$$ain Camera");
script = go.GetComponent<PickupScript> ();
}
I'm using a first person controller, so the camera with the script is parented under another gameobject. Does that change anything? I'm sorry that I didn't tell this to you to start with, but you're being a great help! Thanks, and hope you still want to help me.
Alright, I was able to find a solution. Ins$$anonymous$$d of searching for a specific object, I can just specify it in the inspector if I set it as a public variable. Your information was incredibly valuable, and I would probably still be stuck if it wasn't for you. Thank you so much!
Your answer
![](https://koobas.hobune.stream/wayback/20220613142141im_/https://answers.unity.com/themes/thub/images/avi.jpg)