6

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
  • enabled/disabled is for components Commented Mar 26, 2016 at 18:04
  • You should look up answers before asking them. This question was asked and answered countless times. Commented Mar 26, 2016 at 20:42

2 Answers 2

12

You can use GameObject.SetActive() function to activate or deactivate a GameObject (I think GameObject.enabled was in the old API):

Bouclier.SetActive(false);

By the way, if you want to know the current activation state of a GameObject, use GameObject.activeSelf, which is a read only variable:

Debug.Log(Bouclier.activeSelf);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! That's one thing that was wrong, but I think there is more... I can't seem to be able to read the value of "bouclier" at the Player level. Is there something else obviously wrong in my script?
What happens with you exactly, can you talk more?
I'm making a little video to explain what's up, it will be up in a minute or two :)
Here's the vidéo! youtube.com/watch?v=N0dAZw52Hlk Should I ask another question?
I see that you are changing the variable bouclier in the script Move, but what about the variable bouclier in ShowBouclier script, is there any relation between the two? I think you should change ShowBouclier.bouclier not Move.bouclier .
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.