I am trying to make a solar system and i am trying to set the stars and find them with “FindWithTag”.
Every time run the game it generates this error:
NullReferenceException: Object reference not set to an instance of an object
ShowInfoScript.Update () (at Assets/Scripts/ShowInfoScript.cs:42)
It points to the place where i set the rotation of the star with a Quaternion.
Code:
using UnityEngine;
using System.Collections;
public class ShowInfoScript : MonoBehaviour {
public GameObject camera;
private GameObject mercury;
private GameObject venus;
private GameObject earth;
private GameObject mars;
private GameObject jupiter;
private GameObject saturn;
private GameObject uranus;
private GameObject neptune;
void start(){
mercury = GameObject.FindWithTag ("Mercury");
Debug.Log( "find");
venus = GameObject.FindWithTag ("Venus");
Debug.Log( "find");
earth = GameObject.FindWithTag ("Earth");
Debug.Log( "find");
mars = GameObject.FindWithTag ("Mars");
Debug.Log( "find");
jupiter = GameObject.FindWithTag ("Jupiter");
Debug.Log( "find");
saturn = GameObject.FindWithTag ("Saturn");
Debug.Log( "find");
uranus = GameObject.FindWithTag ("Uranus");
Debug.Log( "find");
neptune = GameObject.FindWithTag ("Neptune");
Debug.Log( "find");
}
void Update (){
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.name == "Earth" ){
mercury.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
venus.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
earth.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
mars.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
jupiter.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
saturn.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
uranus.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
neptune.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
OrbitScript.exeleration = 0;
Debug.Log( "Earth");
}
}
}
}
}
TLDR:
Can’t find my objects using FindWithTag.
Please help me i need it for school…
Ethan