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