In Unity, how can I pass values from one script to another?

后端 未结 4 1008
陌清茗
陌清茗 2020-11-27 18:48

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 tryin

相关标签:
4条回答
  • 2020-11-27 19:27

    if it is in the fixedUpdate – Martin j

    seems like it works well in Awake too. Both scripts are added to the same GameObject.

    public class FirstScript : MonoBehaviour {
        protected internal GameObject myobject;
    private void Awake() {
           myobject = (GameObject)Instantiate(Resources.Load("nameofprefab"));
           myobject.transform.parent = gameObject.transform;
    ...
    
    public class SecondScript : MonoBehaviour {
        private GameObject myobject;
    private void Awake() {
           myobject = gameObject.GetComponent<FirstScript>().myobject;
           myobject.SetActive(false); //for example
    ...       
    
    0 讨论(0)
  • 2020-11-27 19:30
    other.GetComponent<class name>().object name;
    
    0 讨论(0)
  • 2020-11-27 19:38

    There are several ways to achieve this.

    If you want the speed variable controlled by a component which is attached to a GameObject 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:

    go.SendMessage("GetFallingSpeed");
    

    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 Unity singleton manager classes

    0 讨论(0)
  • 2020-11-27 19:48

    If I were you I would just make this speed variable "static public" so you can access it from anywhere. You should always avoid "find.anything" etc functions, they are quite slow. There is no reason for you to look for something that you exactly know where it is.

    0 讨论(0)
提交回复
热议问题