Skip to main content

How can I access a variable from another script in Unity?

I want to be able to use a variable from one script in another script. My goal is to allow me to reference the particleScale variable and use it to affect the size of the object connected to the second script. I also ant to later reference other variables from other scripts. There also will be several instances of each object. This is my first script;

public class Particle : MonoBehaviour
{
    
    public float particleSize;
    public Transform particle;

    void Start()
    {
        particle.localScale *= particleSize;
    }
}

This is my second;

public class Magnetic : MonoBehaviour
{
    
    public Transform magnetic;

    void Start()
    {
        magnetic.localscale *= Particle.particleSize;
    }
}

Help!

Answer

Try this:

public class Particle : MonoBehaviour
{
    public static Particle instance;
    public float particleSize;
    public Transform particle;

    void Awake()
    {
      instance = this;
    }
    void Start()
    {
        particle.localScale *= particleSize;
    }
}


public class Magnetic : MonoBehaviour
{
    
    public Transform magnetic;

    void Start()
    {
        magnetic.localscale *= Particle.instance.particleSize;
    }
}
Other helpful answers

there are few ways to do it,

  1. Using the FindObjectOfType()
public class Magnetic : MonoBehaviour
{
    public Transform magnetic;
    Particle particle

    void Start()
    {
        particle = FindObjectOfType<Particle>();
        magnetic.localscale *= particle.particleSize;
    }
}
  1. Using a singleton pattern (only if you are just going to have a single object of type Particle)
public class Particle : MonoBehaviour
{
    public static Particle Instance;
    public float particleSize;
    public Transform particle;

    void Awake() {
        if (Instance != null) {
          Destroy(gameObject);
        } else {
          Instance = this;
        }
    }
    void Start()
    {
        particle.localScale *= particleSize;
    }
}

then the class will globally accessible from Particle.Instance so you can use any public methods from it or in your case the Particle.Instance.particleSize

Generally speaking you don't want your variables to be public

What you really want to do is to have those variables as private and access them through a public method.

If you need to access your variables from the Unity Inspector it is still a better idea to use [SerializeField]

public class Particle : MonoBehaviour
{

    private float particleSize;
    [SerializeField] private Transform particle;

    void Start()
    {
        particle.localScale *= particleSize;
    }

    public float GetParticleSize() {
         return particleSize;
    }
}

And the way you call them from a different class could be through the usage of FindObjectOfType<> like others have said OR you could create a gameobject with the script attached to it and create a public reference to it in the script of your second gameObject and assign it in the inspector!

public class Magnetic : MonoBehaviour
{

    public Transform magnetic;
    public GameObject particleOb;
    void Start()
    {
       magnetic.localscale *= particleOb.GetComponent<Particle>().GetParticleSize;
    }
}

In this last way of doing it you basically assign the particle GameObject in the inspector and grab the script from it's components. This method also allows you to have multiple objects with few modifications on each object, but still calling the same methods!

Comments