• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by eitanwass · Apr 13, 2016 at 03:34 PM · rotate objectfindfindgameobjectswithtagfindwithtagstars

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eitanwass · Apr 13, 2016 at 04:03 PM 0
Share

I tried "find" before and it gave the same error. Yes. they are tagged as such. Ethan

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

55 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FindWithTag error 0 Answers

How can I count the number of prefab clones? 0 Answers

continuously Find closest with tag, in update. 2 Answers

FindGameObject assign in a variable? help! 2 Answers

Referencing variable from another script on another object 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges