Find with tag not working
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
Answer by rhanza · Jan 03, 2018 at 01:50 AM
Method naming is wrong in your code
void start(){
Just rename your method like this:
void Start(){
It must start with a capital letter.
Answer by Jessespike · Apr 13, 2016 at 04:02 PM
Are the planets really tagged as such? Or are they just named like that?
Try GameObject.Find
Instead of GameObject.FindWithTag
I tried "find" before and it gave the same error. Yes. they are tagged as such. Ethan
Your answer
![](https://koobas.hobune.stream/wayback/20220612074750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Referencing variable from another script on another object 3 Answers
How Can I assign a gameobject via script? 0 Answers
Find GameObjects With Tag only in a scene. 0 Answers
FindWithTag error 0 Answers