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

后端 未结 4 1057
陌清茗
陌清茗 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().myobject;
           myobject.SetActive(false); //for example
    ...       
    

提交回复
热议问题