- Home /
Script only does what it's supposed to when I exit play mode.
EDIT: To solve it, I stopped trying to change the prefab, instead I looped through all of the cactuses and enabled the first child of them which is the game object that matches the hit boxes proportions.
I'm trying to make an option for hitboxes to be visible in my game, this is the script I wrote:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowHitboxes : MonoBehaviour
{
public Image switchOff;
public Image switchOn;
private bool switchIsOn = false;
public Button turnSwitchOnOff;
public GameObject cactusHitBox;
private bool alreadyClicked;
private void Start()
{
cactusHitBox = cactusHitBox.transform.Find("Sphere").gameObject;
}
private void Update()
{
turnSwitchOnOff.onClick.AddListener(EnableSetting);
}
void EnableSetting()
{
if (alreadyClicked)
{
return;
}
StartCoroutine(PreventMultipleClicks(turnSwitchOnOff));
if (switchIsOn)
{
switchIsOn = false;
switchOff.gameObject.SetActive(true);
switchOn.gameObject.SetActive(false);
HideHitboxes();
}
if (!switchIsOn)
{
switchIsOn = true;
switchOn.gameObject.SetActive(true);
switchOff.gameObject.SetActive(false);
UnhideHitboxes();
}
}
void UnhideHitboxes()
{
cactusHitBox.gameObject.SetActive(true);
Debug.Log("true");
}
void HideHitboxes()
{
cactusHitBox.gameObject.SetActive(false);
Debug.Log("false");
}
IEnumerator PreventMultipleClicks(Button button)
{
alreadyClicked = true;
yield return new WaitForSeconds(0.1f);
alreadyClicked = false;
}
}
What's really weird is that, one, when I click the button it counts it as two clicks, I know because the output when I click it is, "false" and then immediately, "true". And two, once I turn on the setting, nothing happens, I checked the game object for the cactuses hitbox and it was still false, I have no idea why, then when I exit play mode, the hitbox appears (What I'm referring to as the "hitbox" is just a game object that matchs the hitboxes proportion, not the actual hitbox itself), even weirder, the prefab which is what I have the variable 'cactusHitBox' set to doesn't even change, all the instances of it do, even though I never even referenced them.
Answer by benjmitch · Nov 19, 2021 at 11:07 PM
The line switchIsOn = false;
is executed, then the test if (!switchIsOn)
. Both if statements will always evaluate true. Hence, two operations per call to the function. Use else
as second test?
Thanks, that fixed the first problem I had. Any idea about the second one? I'm really stuck on it since it just makes no sense.
I'm sorry but I don't understand the second part well enough to help. Perhaps start with an instance in 'cactusHitBox' rather than a prefab, see if that does what you'd expect?
Your answer
Follow this Question
Related Questions
Making a FPS, my gun floats sideways in front of me? 1 Answer
Unity Coroutine problem 1 Answer
Targetting script bug. (Javascript) 0 Answers
Dynamic Font On Android Can't display some texts 4 Answers
question about instantiating 2 Answers