I’m relatively new to C# and Unity.
In this script I made a public bool which is linked to a public boolvalue I made. The public boolvalue does work, but in this specific script, i wanted to check if my player is in range of certain Tags AND my public bool is false. Then a context clue will appear above my Player.
If i collide with a door or an item, or… my context clue comes on. So that part works. The problem is, my context clue comes on anyway, also when my public boolvalue was TRUE. Do I have to update my boolvlue? Or is there something wrong with my if statement?
Thanks for taking your time,
Geert
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ContextClue : MonoBehaviour
{
public GameObject contecxtClue;
public bool playerInRange;
public BoolValue inFight;
private void OnTriggerEnter2D(Collider2D collision)
{
if ((collision.CompareTag("Door") || collision.CompareTag("Interactable") || collision.CompareTag("NPC") ||
collision.CompareTag("Item") || collision.CompareTag("Room Transfer")) && !inFight)
{
playerInRange = true;
contecxtClue.SetActive(true);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if ((collision.CompareTag("Door") || collision.CompareTag("Interactable") || collision.CompareTag("NPC") ||
collision.CompareTag("Item") || collision.CompareTag("Room Transfer")) && !inFight)
{
playerInRange = false;
contecxtClue.SetActive(false);
}
}
}
A “BoolValue” is not a thing that exists in C# or Unity by default - can’t really help you there. If you’re following a tutorial, make sure this “BoolValue” type is implemented properly.
I’m going to go out on a limb and say ‘BoolValue’ is a scriptable object that stores a boolean value.
However, right now, you’re only checking whether or not ‘inFight’ exists. Ergo, whether you’ve assigned it in the inspector. You want to be checking the actual boolean value stored inside the object is true or false.
Possible, but if it’s a ScriptableObject it could have overridden the bool conversion operator itself and take the bool value into account. Though those are all just speculations and without knowing what BoolValue is and how it looks this is kinda pointless.
Just checking if (inFight) only checks if the object exists. inFight == false won’t work without you supplying additional operators.
Say your scriptable object is like this:
[CreateAssetMenu(menuName = "Variables/Boolean Value")]
public class BoolValue : ScriptableObject
{
[SerializeField]
private bool boolValue;
public bool Value { get { return boolValue; } set { boolValue = value; } }
}
Thus you need to check inFight.Value == true/false to check the boolean value.
You can make inFight == false or similar work by overriding operators or providing implicit/explicit operators, but, I wouldn’t worry about that just yet.