Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Albert-GameDev · Mar 11, 2017 at 10:18 AM · c#unity 5gameobjectscipting

GameObject.Find not finding game objects in the scene?

Hi there!

I am trying get a boolean value from another script using GameObject.Find but it is not working.

In the scene I have a horse running around in a track using autopath. I want to make this horse jump when I click on a button. In the horse itself I have a "HorseScript" that holds a boolean that will toggle the horse jump animation.

I have a TestCube in the scene with a script called "Touchable" that is supposed to act as the button.

Right now, I am having an error that says that my "animateHorse" does not exist in the current context. It seems that GameObject.Find can't find the "Horse" game object in the scene and because of that I can't pull the HorseScript in it. I have tried finding other game objects with the other scripts but it is not working either. Please help!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Touchable : MonoBehaviour {
 
     public Color defaultColor;
     public Color selectedColor;
     private Material mat;
 
     void Start()
     {
         mat = GetComponent<Renderer>().material;
     
     }
 
     void OnTouchDown()
     {
         if (GameObject.Find("Horse").GetComponent<HorseScript>().animateHorse)
         {
             animateHorse = true;
             mat.color = selectedColor;
 
         }
     }
     void OnTouchUp()
     {
         mat.color = defaultColor;
     }
     void OnTouchStay()
     {
         mat.color = selectedColor;
     }
     void OnTouchExit()
     {
         mat.color = selectedColor;
     }
 
 }
 
Comment
Add comment · Show 1
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 tanoshimi · Mar 11, 2017 at 10:26 AM 0
Share

Please post the exact error you get. From your description it sounds like Find() is working, but either the HorseScript component can't be found or that it doesn't have a publicly-accessible animateHorse member variable. You can also investigate yourself by breaking the following line into separate steps:

 GameObject.Find("Horse").GetComponent<HorseScript>().animateHorse

First, try finding the GO, and Debug.Log if found. Then try accessing the component and Debug.Log if found etc. etc. This is generally a good strategy for debugging any issues like this.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Mar 12, 2017 at 12:42 AM

change this function in your script to look like this:

  void OnTouchDown()
          {
              GameObject.Find("Horse").GetComponent<HorseScript>().animateHorse=true;
              
                  
                  mat.color = selectedColor;
      
              
          }
 
 
Comment
Add comment · 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
0

Answer by ObscuredCrow · Mar 12, 2017 at 05:54 AM

Based on the information you gave, I usually only have a issue with Find() not working when you don't prepare it ahead of time. Also from what you If statement looks like your not asking it a yes/no question, which is the only thing it will take.

Here is a replacement of your void OnTouchDown() method.

 void OnTouchDown()
 {
      GameObject animHorse = GameObject.Find("Horse").GetComponent<HorseScript>();

      if (animHorse.animateHorse == false)
      {
           animHorse.animateHorse = true;
           mat.color = selectedColor;
      }
 }

This should work for setting your horse animation to true and changing the color you have selected. Just make sure when you're using a if statement that you make it a yes/no question to get the response you're looking for.

If you're looking to have a working code and don't need the use of the if statement then this code will work just as well.

 void OnTouchDown()
 {
       GameObject animHorse = GameObject.Find("Horse").GetComponent<HorseScript>();
 
       animHorse.animateHorse = true;
       mat.color = selectedColor;
 }
Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling an Audio Source on one game object from a script on another game object..? 1 Answer

transform.SetParent(null); Not Working 1 Answer

How to destroy a particular clone on touch/mouse? 1 Answer

Multiple Cars not working 1 Answer

Access gameobjects 2 Answers


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