- Home /
Bool always starts as true, but I want it to start as false
I'm going for a very simple RPG-type minigame here. This is the essence of my "enemy" script, in which I set the enemy's trigger as the "battleRange." When the player is inside an enemy's battle range, it can "hit" the enemy with the space bar and deplete its HP. It works as expected, except for one detail: when I start the game, the bool is set to "true," allowing the player to spam the space bar as soon as the game starts and kill the enemy without getting close to it (obviously no good). Nothing I have tried works to initialize it to "false" on startup (I've tried A: initializing it as battleRange=false, as well as battleRange=true, just for kicks, B: initializing it in the Start function instead, C: leaving it uninitialized at all). I'm sure I'm doing several things wrong, as I still don't totally understand OnTriggerEnter/Exit. Should I be putting parts of this on the player rather than the enemy? All suggestions welcome, thank you kindly!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float enemySpeed = 3;
Rigidbody enemyRB;
GameObject player;
public bool battleRange = false;
public int hP = 5;
void Start()
{
enemyRB = GetComponent<Rigidbody>();
player = GameObject.Find("PLAYER");
//battleRange = false;
}
void Update()
{
enemyRB.AddForce((player.transform.position - transform.position).normalized * enemySpeed, ForceMode.Impulse);
if (Input.GetKeyDown(KeyCode.Space) && battleRange)
{
hP -= 1;
}
if (hP < 1)
{
Destroy(gameObject);
}
//Debug.Log(hP);
Debug.Log(battleRange);
}
private void OnTriggerEnter(Collider other)
{
battleRange = true;
}
private void OnTriggerExit(Collider other)
{
battleRange = false;
}
}
Answer by ShervinM · Apr 25, 2021 at 11:11 PM
There could be two possible scenarios at play here.
Scenario 1:
This is a monobehaviour script, and by default, public variables are serialized. So what could possible be happening is that the first time you attached this script to your object, public bool battleRange
was set to true. If that was the case, changing your code to initialize it to false via public bool battleRange = false;
won't actually change the value of anything thats already serialized (ie objects that have this script already attached). It will only initialize that value to false for new scripts that are attached (or if you reset this component on an existing object). In this case, the first thing to check is to select your gameobject, and visually see in the inspector whether or not battleRange
is set to true or false.
Scenario 2:
You've done the above (unchecked the variable via the inspector on the component), and at run time battlerange is reset to true again. In this case, whats almost certainly happening is that at runtime OnTriggerEnter is getting called and setting your variable to true (you can test this by putting a breakpoint on the line where you assigne battleRange to true). In general, its a good idea to check through some conditions in OnTriggerEnter/Exit callbacks. In otherwords, you don't want to set battleRange
to true every single time any collider comes in contact. You'll likely want to query what other
is to make sure its the player/whatever specific collider before you set your variable. This is often done by other checking what other
's tag is or by reference checks.
Your answer
Follow this Question
Related Questions
Unity3D: Entering a trigger disables a few bools for no reason 2 Answers
How to get back player control? 0 Answers
zooming in and out with mouse wheel 2 Answers
How to make my animated axe collide with tree properly?? 0 Answers
I need help fixing this code 1 Answer