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
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!
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 RotateInto$$anonymous$$oveDirection()
{
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 $$anonymous$$ching 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?
@dan5071 your answer is perfect, but there is a little problem..
transform.Find only finds immediate children. What should I use in order to access a child that is deeper inside the parent and not just first tier children?
Your answer
Follow this Question
Related Questions
(Solved) Findout if an object exist in the scene, but dont trow exception 1 Answer
How Can I assign a gameobject via script? 0 Answers
setting a variable to a GameObject that has a certain RGB value??? please help 0 Answers
Trouble with GameObject.Find() After LoadLevel.Load() 0 Answers
How do I check if a child object exists? 5 Answers