Take the 2-minute tour ×

In Unity, I want one object to have a falling speed variable that all the other objects can access. For various reasons, I can't use the inbuilt gravity for what I'm trying to do.

How can I access a variable in one object repeatedly, so that when it updates I get the updated variable, from another object?

share|improve this question
Declare it in the global scope? I thought Unity used javascript, but the C# tag throws me off. –  Jan Dvorak Dec 15 '12 at 11:52
@JanDvorak In Unity you can use UnityScript (their version of javascript) or C#. –  Ergwun Dec 15 '12 at 11:55
Unity can use JavaScript, C~ or Boo (a pythonesque, fairly new language) –  Dakeyras Dec 15 '12 at 11:55
I believe c# has a static (class-local) scope that you can use. Perhaps this is where you should place it. –  Jan Dvorak Dec 15 '12 at 11:56
I need to define a new class for every script though. Within a script, I can easily pass values, I just don't know how to do it from outside. –  Dakeyras Dec 15 '12 at 11:58
add comment

1 Answer

up vote 8 down vote accepted

There are several ways to achieve this.

If you want the speed variable controlled by a component which is attached to a class MyObject

public class SpeedController : MonoBehaviour
    public float speed;
    // maybe you want restrict this to have read access, then you should use a property instead

In other classes you can do:

GameObject go = GameObject.Find ("MyObject");
SpeedController speedController = go.GetComponent <SpeedController> ();
float courrentSpeed = speedController.speed;

Take care that there is one object named MyObject only otherwise things get messed up.

Alternatively you can define a SpeedController member in every class that needs access to speed and set a reference via drag and drop in Unity editor. You save the lookup then but of course this is pretty inconvenient if needed in many classes.

Another way is to create a singleton which holds the speed variable and have:

public class MyGlobalSpeedController {
    private static MyGlobalSpeedController instance = null;
    public static MyGlobalSpeedController SharedInstance {
        get {
            if (instance == null) {
                instance = new MyGlobalSpeedController ();
            return instance;
    public float speed;

So all classes can access this:

float currentSpeed = MyGlobalSpeedController.SharedInstance.speed

As Jan Dvorak stated in comments section:

public class SpeedController : MonoBehaviour
    public static float speed;

[Update] Thanks to Jerdak. Yes Component.SendMessage should be definitely on the list:


Again you need to have a reference to go like described in the first solution.

There are even more solutions to this problem. If you are thinking of game objects that are active in all scenes, you should have a look at Unity3D singleton manager classes

share|improve this answer
Don't forget SendMessage It's def. not the best option but it's useful on occasion. –  Jerdak Dec 15 '12 at 14:12
@Jerdak thanks, I updated my answer. –  Kay Dec 15 '12 at 14:31
I keep getting this error: 'error CS0236: A field initializer cannot reference the nonstatic field, method, or property' (I am using your first method) –  Dakeyras Dec 15 '12 at 21:05
Here: SpeedController speedController = go.GetComponent<SpeedController>(); –  Dakeyras Dec 17 '12 at 17:17
@Dakeyras Seems like you are doing it in initialisation of a member variable. Maybe A realy easy c# question sheds some light on it. –  Kay Dec 17 '12 at 19:45
show 2 more comments

Your Answer


