Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • 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
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

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 /
avatar image
0
Question by StegnerGames · Dec 05, 2017 at 10:55 PM · cameras

`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;
             }
         }
     }
 }
 
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

2 Replies

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

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.

Comment
Add comment · Show 5 · 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 StegnerGames · Dec 06, 2017 at 01:29 AM 0
Share

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.

avatar image MacDx StegnerGames · Dec 06, 2017 at 01:42 AM 0
Share

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?

avatar image StegnerGames MacDx · Dec 06, 2017 at 01:46 AM 0
Share

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.

Show more comments
avatar image
2

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.

Comment
Add comment · Show 2 · 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 KulNar282 · Sep 25, 2020 at 01:49 AM 0
Share

@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)

avatar image ChrisP999 KulNar282 · Mar 22 at 07:15 AM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

73 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 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges