- Home /
`UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name instead
I get the following error on lines 29 and 32
`UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name instead
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Spectate : MonoBehaviour {
private Camera Main_Cam;
private Camera Spectate_Cam;
private GameObject cam_main;
private GameObject cam_spectate;
private GameObject gui;
private GameObject player_name;
private GameObject name_player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Overview(){
foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player")){
if (player.GetComponent<Renderer>().material.GetColor("_Color") == Color.blue){
cam_spectate = player.Find ("Spectate_Camera");
Spectate_Cam = cam_spectate.GetComponent<Camera> ();
// cam_main = player.GetComponentInChildren<Camera> ();
cam_main = player.Find ("Main Camera");
Main_Cam = cam_main.GetComponent<Camera> ();
// Main_Cam = player.GetComponentInChildren<Camera> ();
gui = GameObject.Find ("GUI");
if (Main_Cam.enabled == true) {
if (Network.isServer) {
// Main_Cam.enabled = (false);
player.SetActive (false);
Spectate_Cam.enabled = (true);
gui.SetActive (false);
} else {
Main_Cam.enabled = (!Main_Cam.isActiveAndEnabled);
Spectate_Cam.enabled = (!Spectate_Cam.isActiveAndEnabled);
}
} else {
if (Network.isServer) {
// Main_Cam.enabled = (false);
player.SetActive (false);
Spectate_Cam.enabled = (true);
gui.SetActive (false);
} else {
Main_Cam.enabled = (!Main_Cam.isActiveAndEnabled);
Spectate_Cam.enabled = (!Spectate_Cam.isActiveAndEnabled);
}
}
break;
}
}
}
}
Answer by MacDx · Dec 06, 2017 at 01:15 AM
Find is a static method of the GameObject class, and as such it can't be called by instances of the class it can only be called by the type itself. So the console is telling you to do exactly that.
//Instead of doing this:
player.Find ("Whatever");
//You need to do this:
GameObject.Find("Whatever");
Just like you are already doing on line 35 to find your gui.
That doesnt work. Nothing happens when I press the button. I have 2 cameras as children of the player; the main camera and spectate camera.
That doesnt work.
You mean it solved the compilation error you had but it doesn't do what you expected it to do afterwards. Although that's completely unrelated to the question asked, I'm willing to help. Could you describe what kind of game you are making, and what exactly do you expect to happen when you press the button and how's that different from what's happening right now? Also, are you using Unity's networking system?
I'm not really making a game per say, I'm just playing around with Unity. What I expect to happen when the button is pressed is the main camera and spectate camera toggle. What happens right now is nothing. I am using Unity's networking system.
Answer by Incogitance · May 18, 2020 at 11:18 PM
I have found that several people have been looking for an answer like this.
What helped me was
// Instead of
player.Find("Whatever");
// You can use
player.transform.Find("Whatever");
Gameobject.Find() will search through the whole scene and is not useful when using clones.
@Incogitance I'm trying to access the SelectionCircle child GameObject of my unit, but putting gameObject.transform.Find("SelectCircle")
or this.gameObject.transform.Find("SelectCircle")
in the Unit class gives me a "Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.GameObject'" error. Do you know why this might be? (I may have already posted this, but it disappeared for me)
Hi, your problem is transform.find returns a Transform, not a gameobject, but the transform contains the gameobject if that's what you need. (I had exactly the same issue)
Your answer
Follow this Question
Related Questions
Raycast with multiple camera's in scene 0 Answers
Platformer - Pan camera when player moves near the edges? (c#) 0 Answers
capture photo with text 0 Answers
Multiple Cameras with Steam VR, Unity 5.4 3 Answers
"Warning: Display 1: No cameras rendering" when all cameras render to RenderTexture 1 Answer