• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 TomatuAlus · Feb 13, 2016 at 06:00 PM · gameobjectfindfind-gameobject

GameObject.Find throws be a error when trying to find a child.

Here is the code -

 using UnityEngine;
 using System.Collections;
 
 public class MoveEnemy : MonoBehaviour {
 
     private GameObject Road1;
     public Sprite NE;
     [HideInInspector]
     public GameObject[] waypoints;
     private int currentWaypoint = 0;
     private float lastWaypointSwitchTime;
     public float speed = 1.0f;
     public string  waypoint;
     void Start () {
         lastWaypointSwitchTime = Time.time;
         Road1 = GameObject.Find("Road_1");
     }
 
 //~~~~~ some code here which isnt relevant. Update calls the method //RotateIntoMoveDirection. ~~~~~~~~
 
 
     private void RotateIntoMoveDirection()
     {
         waypoint = currentWaypoint.ToString();
         Debug.Log(waypoint);
         Road1.Find("waypoint");
     }
 }

This code calls out the error - ''Assets/Main Project/Scripts/MoveEnemy.cs(67,15): error CS0176: Static member `UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name instead''

My search has found out that i have to use GameObject instead of gameObject, but i AM using it!

I am trying to find which waypoint is currently passed. I get the number of it and convert it into string and try to find the child with the name same as the index of the waypoint.

If waypoint is 1 then i find child with index of 1 in the GameObject. Then i will find get its name and change other gameobjects sprite to its name. If it's taxi_NE i will change it to sprite taxi_NE and so on

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by dan5071 · Feb 14, 2016 at 12:39 AM

I'm afraid GameObject.Find doesn't quite work like that. I'd imagine the line with

 Road1.Find( "waypoint" );

is line 67 in your code as specified in your error message. What you're doing here is looking for a method in a local instance of the GameObject class, identified as a gameObject when you write code (note the difference in the uppercase and lowercase G's). Using the GameObject class is kind of like having a mold that you use to make gameObjects. These are the objects that actually appear in your game. The GameObject is an abstract concept where a gameObject is a concrete instance. The latter inherits the general features of a GameObject such as having a Transform component. However, some methods such as Find cannot be accessed through a gameObject that you made from the GameObject "mold." This is why you are able to say Road1.SetActive(), but not Road1.Find(). Some features are inherited by the gameObject, but others are not.

Instead, you need to use the class GameObject. Change that line to :

 GameObject.Find( "waypoint" );

and it should work as long as you don't have multiple game objects in your scene labelled "waypoint." It will only work in this case because GameObject.Find will search the entire scene for objects with that name. However, if you would need to specifically access objects that are children of Road1, you could use:

 Road1.transform.Find( "waypoint" );

Since children are actually controlled by the transform component of a gameObject. That may have been a long explanation but I hope it helps!

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 TomatuAlus · Feb 14, 2016 at 09:36 AM 0
Share

Oh well, thanks anyways, but this didn't help like i hoped to. I fixed my problem this way

     public Sprite NW;
     public Sprite NE;
     public Sprite SW;
     public Sprite SE;
     public GameObject SpriteObject;
     private SpriteRenderer spriterenderer;
 
     void Start () {
         spriterenderer = SpriteObject.GetComponent<SpriteRenderer>();
           }
        
  private void RotateIntoMoveDirection()
     {
        
         switch (currentWaypoint)
         {
             case 1:
                 spriterenderer.sprite = NE;
 
                 break;
             case 2:
                 spriterenderer.sprite = NW;
                 break;
             // case 3 and so on
         }
     }
 }

Originally i wanted to find the INDEX of the child, but it seems like this is just too much of a hassle i have created for myself as always. Big thanks for teaching me this much!

Sorry for asking for more but there is one more question i have here http://answers.unity3d.com/questions/1140511/lerp-on-the-every-3-n-cycle-does-some-wierd-thing.html which is a wierd bug/interaction i have in my lerp. Care on trying to answer that?

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

48 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

Related Questions

(Solved) Findout if an object exist in the scene, but dont trow exception 1 Answer

GameObject.Find doesn't return elements of new scene 0 Answers

Center of a GameObject with Bounds? 1 Answer

Talking to GameObject and components problem 1 Answer

Way of Finding/accessing a variable from another script though another variable? 1 Answer

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