lyona
lyona

Reputation: 25

How to enable and disable an object when something happens

The player changes its color after passing through every wall. If the player is Yellow and the wall is Red then, the color of the player becomes orange. Also the next wall will be spawned with a little orange part. However, I could not turn on and off the orange wall object. The code below changes the players color and also changes the wall's color. However I could not make it acitve the wall (Which is deployBlack) and after passing through it deactivating it. Here is my code :

if(col.tag!= currentColor){
        if (currentColor=="Yellow" && col.tag=="Red"){
            sr.color=colorOrange;
            currentColor="Orange";
            deployBlack.SetActive(true);
            whitesr.color=colorOrange; 
            deployBlack.SetActive(false);
      }

Upvotes: 2

Views: 177

Answers (1)

Jee
Jee

Reputation: 977

Okay got it. Now that I see the rest of your code it's a lot easier to diagnose the problem. One of your if statements that occur OnTriggerEnter2D will eventually execute and when it does, it sets deployBlack to true but then immediately after that true statement it sets it to false every single time. Therefore you will see it until you pass through the wall one time. Once you do, it will end any if statement with the deployBlack set to false. This means that no matter what color anything is, the end result of the deployBlack will be false. You need to place the deployBlack.SetActive(false); somewhere else in your code.

I'm still a bit unsure as to what you are trying to achieve because I don't know how you are spawning walls, or what variable/sprite is the player, but regardless, your core issue is the fact that every if statement ends with deployBlack.SetActive(false); so it will always appear as false except for the couple of milliseconds that it is set to true.

Hope this helps!

Upvotes: 1

Related Questions