- Home /
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;
}
}
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.
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;
}
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;
}