Answer the question
In order to leave comments, you need to log in
How to fix an error when searching in an instantiate object?
Hello, I have a simple tank prefab that was created at the beginning of the game. I want to find a child gameObject in it
public class GameManager : MonoBehaviour {
public GameObject tankPrefab;
private GameObject playerTank;
// Start is called before the first frame update
void Start () {
GameObject inst = Instantiate (tankPrefab, new Vector3 (0, 0, 0), Quaternion.identity);
playerTank = inst.gameObject.Find ("Body");
}
// Update is called once per frame
void Update () { }
}
Assets\Scripts\GameManager.cs(14,22): error CS0176: Member 'GameObject.Find(string)' cannot be accessed with an instance reference; qualify it with a type name instead
Answer the question
In order to leave comments, you need to log in
Because find is a static method, it should not be called on an instance.
This is how it should work
playerTank = inst.gameObject.transform.Find("Body");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question