Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(+1)

"Trying to access a variable from another script has been oddly difficult."

Yeah, I searched for a LONG time until I finally found a tutorial...

So if anybody's interested, here's how u do it (c#)

SCRIPT 1

public class Script1 : MonoBehaviour // this thing is here by default
{
    public float CoolVariable = 1; // u need to put a "public" before the var type so that it can be accessed by other scripts
}

SCRIPT 2

public Script1 Script1;  // after the "public" you need to write the name of the other script (that you can find in the "public class ___ : MonoBehaviour")  thing and after you do that you write how you want to address to the script, but I recommend just calling it by its name (so that's why I wrote script1 twice)
//then go to unity and assign the object that has the script containing the variable that you want to access, or, if it is in the same object as script 2, just directly assign the script.
Debug.Log(Script1.CoolVariable)
//and boom, you got the variable from  another script.

The way I did it was much more confusing. Good tutorial. Here's my code if you were wondering.

Debug.Log(GameObject.Find("GAME OBJECT WITH TARGET SCRIPT").GetComponent<Script1>().CoolVariable);

(+1)

ooh ok

(+1)

Also, If you just want the code without the comments, here it is

SCRIPT 1

public class Script1 : MonoBehaviour
{
    public float CoolVariable = 1;
}

SCRIPT 2

public Script1 Script1;
Debug.Log(Script1.CoolVariable)