Accessing a non static variable...

Hey guys !
Trying to make a cookie -clicker-like game and I’m having troubles accessing variables between scripts.
Here is the deal :

  • In one script, I have a public non-static variable of type int.
  • I need to access it from another script that is in the same gameobject.
  • The gameobject containing the scripts aforementioned comes from a Prefab, so many objects will share those scripts and I can’t use a static variable.
  • I tried using a reference but I couldn’t do anything to the variable because of its type…

So here is my first script :

public class Achat : MonoBehaviour {
    public int NombreUnité;
}

And I need to use that variable in another script of the same object, without refering to that gameobject’s name…
Maybe I can’t find the solution just because I’ve been working on it the whole afternoon… anyway if someone could help that would be nice. :wink:
Clap

If it’s on the same object you can just use:

int newVariable = GetComponent<scriptname>().NombreUnite;
2 Likes

It’s kinda frustrating to see how easy it was in the end ^^
What’s weird is that I tried that twice, the first time it errored me because “newvariable” had the wrong type. I cleaned the program, rebuilt it and then it worked ! :o
Thanks a lot anyway ! :smile:
Clapi.