I have an object with a speed field. I want other objects to be able to use its value, that is, in fact, to have access to the script variable (for various reasons this may be needed).
How can I access a variable of an object so that all changes that have occurred in it can be obtained from another object?
The question is translated from https://stackoverflow.com/q/13891892/6104996
Answer 1, authority 100%
There are several ways to do this.
If you want to get the speed
value from a component that is attached to a named object, suppose MyObject
:
public class SpeedController: MonoBehaviour
public float speed;
// To restrict read-only and prohibit writing - you can use "Property"
then in another component you can do the following:
// Find an object by name
GameObject go = GameObject.Find ("MyObject");
// take its component where the speed lies
SpeedController speedController = go.GetComponent & lt; SpeedController & gt; ();
// take the speed variable
float courrentSpeed = speedController.speed;
Note : if there is no object with such a name, or there are many of them, this can lead to disastrous consequences. Also keep in mind that the Find
operation is quite slow. And, for example, in Update
it is best not to use it.
It is better to find the object at startup and only then use it.
The most inconvenient way (and, in principle, incorrect, but valid) is to declare a variable speed
of type SpeedController
in each class. Drag an object in the editor into this field. Then you can monitor the state of the variable at any time. But the way is terrible and not recommended
Another way to create a singleton that will contain your speed
variable:
public class MyGlobalSpeedController {
private static MyGlobalSpeedController instance = null;
public static MyGlobalSpeedController SharedInstance {
get {
if (instance == null) {
instance = new MyGlobalSpeedController ();
}
return instance;
}
}
public float speed;
}
Then all classes will be able to access through it:
float currentSpeed = MyGlobalSpeedController.SharedInstance.speed
You can also declare a variable static and then it will not be tied to the object, but will belong to the class
public class SpeedController: MonoBehaviour
public static float speed;
it’s easy to access:
var speedVal = SpeedController.speed;
Debug.Log (speedVal);
Since you are trying to make it available to all other objects, then, I assume that there is one object with such a variable on your stage. Otherwise, this method becomes useless
You can also use the message system in Unity
For example Component.SendMessage . It has 4 overloaded methods
public function SendMessage (methodName: string, value: object = null, options: SendMessageOptions = SendMessageOptions.RequireReceiver): void ;
public function SendMessage (methodName: string, value: object = null, options: SendMessageOptions = SendMessageOptions.RequireReceiver): void;
Public Function SendMessage (Methodname: String, Value: Object = NULL, Options: SendMessageOptions = SendMessageOptions.Requireceiver): void;
Public Function SendMessage (Methodname: String, Options: SendMessageOptions): void;
It calls a method called Methodname
in each monobehaviour
on this object, for example:
go.sendmessage ("getfallingspeed");
will call the getfallingspeed
method on the Go
object. And this method (getfallingSpeed) is possible just and will manage the speed
parameter.
Do not forget that you need to access Go
in any way, i.e. via find
or another convenient way.
response Translated with an addition from https://stackoverflow.com/a/13892844/6104996