Hi, I have a control panel and i want to use it to control the animation of a landing gear…
So this is the script i have written for the control panel which i can control using keydown and…
public class panel : MonoBehaviour {
public bool stick;
private Animator animator;
private GameObject player;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void OnTriggerStay(Collider other) {
if (Input.GetKeyDown (KeyCode.P)) {
animator.SetBool ("stick", true);
}
if (Input.GetKeyDown (KeyCode.O)) {
animator.SetBool ("stick", false);
}
}
this is the script i written for landing gear which i want it to detect the boolean is true or false from the control panel:
public class landinggear : MonoBehaviour {
private Animator anim;
private GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(GameObject.Find("panel").GetComponent<panel>().stick)
{
anim.SetBool("retraction",true);
}
if(!GameObject.Find("panel").GetComponent<panel>().stick)
{
anim.SetBool("retraction",false);
}
}
}
So both object have its own animator, and i succeed to activate control panel but not the landing gear…
and I very new to C# scripting so anyone can stop by and help me with this…
thank you very much.
The problem is that your first script is only changing the ANIMATOR boolean value. NOT the actual boolean value. So you’ll need to change your stuff to
if(Input.GetKeyDown(KeyCode.P)){
stick = true;
animator.SetBool("stick", stick); //We're setting the bool value to the same as the "stick" variable so it only has to be set one time (more dynamic)
}
And do the same for the other one.
Now your landinggear script should work
ALSO at the top of your landinggear script i’d recommend creating a “panel” reference at the top of your script.
So where you have your anim and player add in a
private panel pan;
and in Start()
pan = GameObject.Find("panel").GetComponent<panel>();
and then refer to pan instead of the GameObject.Find blah blah stuff
The reason for this is GameObject.Find(“blahblah”).GetComponent(); is VERY slow and calling it every frame can slow your game down. Setting up a reference to this “panel” you made is one, easier to write/read, and is also much faster
Really thanks for the reply @corn@SterlingSoftworks
So i have do some editing and still it shows error :
NullReferenceException: Object reference not set to an instance of an object
landinggear.Update () (at Assets/landinggear.cs:26)
script for panel:
public class panel : MonoBehaviour {
public bool stick;
private Animator animator;
private GameObject player;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void OnTriggerStay(Collider other) {
if (Input.GetKeyDown (KeyCode.P)) {
animator.SetBool ("stick", true);
stick = true;
}
if (Input.GetKeyDown (KeyCode.O)) {
animator.SetBool ("stick", false);
stick = false;
}
}
script for land gear:
public class landinggear : MonoBehaviour {
private panel pan;
private Animator anim;
private GameObject player;
// Use this for initialization
void Start () {
pan = GameObject.Find ("panel").GetComponent<panel> ();
}
// Update is called once per frame
void Update () {
if(pan.stick)
{
anim.SetBool("retraction",true);
}
if(!pan.stick)
{
anim.SetBool("retraction",false);
}
}
}
p/s: I still very confuse about using code to call upon checking for the control panel boolean is it true or false ( So saying that under landing gear script ; void update ; that part i not sure is it a correct way to check the boolean… ) ( really appreciate you guys can help… thanksss