Enable/disable a GameObject component from a script [Unity3D]

Viewed 31940

I need to take the value of a boolean (put in a variable called "bouclier") set in one script to enable or disable a GameObject.

The variable is in game object Player (bottom right here):

enter image description here

And I need to enable of disable this game object ("Bouclier01"):

enter image description here

To do this, I attached a script to game object "Bouclier01". Here it is:

using UnityEngine;
using System.Collections;

public class ShowBouclier : MonoBehaviour {

    public GameObject Bouclier01;
    public bool bouclier;

    // Use this for initialization
    void Start () {
        Bouclier01 = Bouclier01.GetComponent<GameObject>();
    }

    // Update is called once per frame
    void Update () {

        Bouclier01.enabled = false;
        if (bouclier == true) {
            Bouclier01.enabled = true;
        }
    }
}

I must be missing something, because this comes up with this error message:

enter image description here

Any idea how to properly accomplish this?

2 Answers

it will works

public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
}
Related