So, I'm trying to replace a string from one script with a string from another script, but it gives me an error stating: "Object reference not set to an instance of an object"
Here is my code:
public NavMeshAgent nav;
public Camera cam;
public string selectedPlanet;
void Start () {
nav = GetComponent ();
cam = Camera.main;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse0)) {
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
nav.SetDestination (hit.point);
}
}
if (Input.GetKeyDown (KeyCode.Mouse1)) {
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.tag == "Planet") {
SceneManager.LoadScene (selectedPlanet);
}
}
}
}
---------------------------------------------------------------------------------------------------------------
public string planetName;
public string playerObjectName;
Camera cam;
ShipMovement sm;
void Start () {
cam = Camera.main;
sm = GameObject.Find (playerObjectName).GetComponent ();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse1)) {
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.tag == "Planet") {
sm.selectedPlanet.Replace (sm.selectedPlanet, planetName);
}
}
}
}
If someone could help me figure out why this is happening, it would be greatly appreciated. I really do feel like replacing a string should not be hard. Thanks in advance.
↧