• 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 NutellaDaddy · Jan 23, 2014 at 06:10 AM · c#nametype

GameObject.Find(string) cannot be accessed with an instance reference

qualify it with a type name instead. Whats the problem here? Line 12

 using UnityEngine;
 using System.Collections;
 
 public class PowerUpScript : MonoBehaviour {
 
     HUDScript hud;
     private  string mainCamera = "MainCamera";
 
     void OnTriggerEnter(Collider2D other)
     {
         if (other.tag == "Player") 
         {
             hud = gameObject.Find(mainCamera).GetComponent<HUDScript>();
             hud.IncreaseScore(100);
             Destroy(this.gameObject);
         }
     }
 }
 
Comment
Add comment · Show 4
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 Invertex · Jan 23, 2014 at 07:00 AM 0
Share

hud = gameObject.Find("mainCamera").GetComponent();

You need to put quotes around things that are strings. "Find" requires a string input.

Also, you should just call that line in void Start(){}, so that you don't create that large overhead every time you enter the collider... Find() is an expensive operation, and you only need to find the reference once, at the start of your scene.

Show more comments
avatar image NutellaDaddy · Feb 11, 2014 at 03:23 AM 1
Share

I just wanted to try something new!

1 Reply

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

Answer by GameVortex · Jan 23, 2014 at 09:27 AM

The function **Find** is a static member of the class GameObject, which means that you have to access it using the type instead of the instance:

 hud = GameObject.Find(mainCamera).GetComponent<HUDScript>(); //Notice the upper case G in GameObject to specify the class instead of the instance. 

You appear to be searching for the MainCamera though and as long as your main camera is tagged as MainCamera you can easily access it through the Camera class instead:

 hud = Camera.main.GetComponent<HUDScript>();

And as @Invertex says, you should put that code in a Start() of your script to avoid running it multiple times which is unnecessary .

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 poncho · Jan 23, 2014 at 04:35 PM 0
Share

this is the answer, and the tip to set HUDScript at the Start would help, also, you could have if(hud == null) hud = Camera.main.GetComponent();, just in case.

avatar image JackalopeConspiracy · Mar 11, 2015 at 07:09 PM 0
Share

Hey I'm having a similar problem, and I was wondering if you'd be willing to help me out too GameVortex.

http://i.imgur.com/jI7C0n7.png

and

http://i.imgur.com/r$$anonymous$$zEiYf.png

are some screenshots of my code and stuff. Any help?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Multiple Cars not working 1 Answer

Learning C# instead of doing what tutorials say... 2 Answers

Vote For Name Everyone's Vote Counts 1 Answer

Third Person Multi-player Swapping Controls 0 Answers

UnityEngine.Input.GetMouseButton(1)) issue 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