How do I turn off the previous object?
I'm making a game in the td genre. I made it so that when the character clicks on the tower, the sprite turns on, which shows the range of action, but! if a person clicks on another tower, then the previous one will remain on, how can this be avoided? in all games of this genre, they make it so that if a person clicks on another tower or on the playing field, then the previous tower turns off. how to do it?
I do this on and off, i can't figure out how to do it further
public void OnMouseDown()
{
if(CyrcleUse.activeSelf == false)
{
CanvasTower.SetActive(true);
CyrcleUse.SetActive(true);
}
else if (CyrcleUse.activeSelf == true)
{
CanvasTower.SetActive(false);
CyrcleUse.SetActive(false);
}
}
Answer by Marioooo · Feb 23 at 06:39 PM
Hi! in order to achieve this, you must save a reference to the last tower the player set on...
Set active the tower,
Store the current active tower on a variable (you can use the player script but if you want more complexity you should create a manager for towers)
Player activates a new tower:
The previous tower stored on the variable is set to inactive, the new tower is set to active, and then stored on the varialbe.
First time you do this you should chech with an if if theres a value on the tower variable, if not, just activate and store... if not, then activate and store..
i know im a bit late, hope this helps
Your answer
Follow this Question
Related Questions
2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 0 Answers
Why do prefabs overlap each other? 0 Answers
how to make my 2d top down character face the direction of the cursor 1 Answer
Problem with tracking multiple object with multitouch 0 Answers
Slider.value doesn't show the value in real-time but after the execution of the script 1 Answer