L
L
Ler Den2019-04-12 12:45:34
.NET
Ler Den, 2019-04-12 12:45:34

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 () { }
 }

I get an error
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

How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2019-04-12
@GavriKos

Because find is a static method, it should not be called on an instance.

A
Ark Tarusov, 2019-04-16
@kreo_OL

This is how it should work

playerTank = inst.gameObject.transform.Find("Body");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question