2021年3月3日星期三

How to reference a component of an object in another Scene?

I don't really get help from other posts on this topic, so I search for help focused on my case. I try to make a gamemanager in a scene only for itself. I made it DontDieOnLoad. Now I try to get the script of an gameobject from another scene, but it doesn't get the object.

NullReferenceException: Object reference not set to an instance of an object

public class InputManager : MonoBehaviour  {      Spaceship _spaceship;        private void Start()      {          _spaceship = GameObject.Find("Spaceship").GetComponent<Spaceship>();      }        void Update()      {          if (Input.GetKeyDown(KeyCode.UpArrow))          {              _spaceship.Shoot();          }            if (Input.GetKeyDown(KeyCode.LeftArrow))          {              _spaceship.MoveLeft();          }            if (Input.GetKeyDown(KeyCode.RightArrow))          {              _spaceship.MoveRight();          }        }  }  
https://stackoverflow.com/questions/66467716/how-to-reference-a-component-of-an-object-in-another-scene March 04, 2021 at 10:27AM

没有评论:

发表评论