0

I need to use some variables defined in a script but when I call them their values are 0. I don't know what I am doing wrong.

Example:

Script1.cs

public int cont;


public void Method() { cont++; }

void Update() { Method(); }

Script2.cs

public Script1 usingScript1;

void MethodX()
{
    usingScript1.GetComponent<Script1>();
    Debug.Log(usingScript1.cont);
}

void Update() { MethodX(); }

This script should be showing the "cont" variable increasing since it's being called from Update(), but that's not happening. When I call it, it's 0 and don't increase.

Also, I refer the object which contains Script1.cs in the Ispector. It must be a simple thing that I'm missing. I even tried calling Method().

3
  • I'm voting to close this question as off-topic because it is a zillion-times duplicate. Interestingly it's the single most common question about Unity from new hobbyists trying Unity. Commented Feb 20, 2016 at 1:39
  • 1
    "DO NOT USE EVER Update...experts never need to use it" - Incorrect. "Update is the most commonly used function to implement any kind of game behaviour" Commented Feb 20, 2016 at 4:23
  • Hi @JoeBlow , are you talking about avoiding Update when RigidBodies are involved? In that case I agree with you. Otherwise there is nothing wrong with overriding Update as you can see in my spiffy GPGPU n-Body galaxy simulation. :) Commented Feb 20, 2016 at 14:52

3 Answers 3

2

Just to add to what everyone mentioned, have you tried initializing "cont" ?

This

public int cont;

becomes

public int cont = 0; 

Also try initializing it in the Start() function if this doesn't work.

Sign up to request clarification or add additional context in comments.

Comments

1

The function called Method() is never called anywhere in this code. Since it's the only thing that modifies the value of the variable called cont, if it is never called, cont will never change from its default value of zero.

EDIT: Whoops! Okay, the actual problem here is that you need to change

usingScript1.GetComponent<Script1>();

to

usingScript1 = GetComponent<Script1>();

The latter line of code sets the variable usingScript1 so that you can use it in your code. The former simply calls a function without doing anything with the information it returns.

EDIT: GetComponent() will only work is the two scripts are attatched to the same gameobject. Otherwise, you can use FindObjectOfType().

3 Comments

just because it's not the way that you would do it doesn't mean it's wrong... you don't gotta go downvoting my answers...
Hi Halp! I do not downvote. Your script is wrong ... you're thinking of something like Find("that game object").GetComponent<> ...
If the two scripts are on the same gameobject, GetComponent() will work just fine. I do realize that I should have informed Sean of this in case he was not already aware. But thanks!
0

I managed to solve it and never thought about it. As simple as it. Instad of creating an object, I just made the variables static and call 'em using the class.

Script1.cs

public static cont;

Script2.cs

Script1.cont;

And it works.

1 Comment

As a rule do not do this in Unity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.