- Home /
Bool is Not being called Properly
Hello there I have been working on this code for the past 7 or 8 hours straight and simply cannot figure out what I have done wrong. I have looked at multiple videos forums and anything else I could find so I would highly appreciate some help...
Anyways to the problem, simply put I am trying to create a small game in which a player will trigger a collision with a key and their key count will go up. Once the key count is above or equal to 1 they will be able to move through a gate or door. The door will just disappear for now as I am still learning.
The problem stems from me trying to initiate a static boolean on my character that changes from False to True once they pick up a key. However, each time I start the game it immediately sets the Boolean to True.
I have three scripts: 1. KeyCheck - On Gate 2. KeyItem - On the actual Key 3. KeyCollect - On the player
Attached are two of the scripts and the KeyItem is as follows, as I am only able to add two attachments. I appreciate any help I can get on this >:
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.name == "thePlayer")
{
Destroy (gameObject);
}
}
}
Answer by The_Three_Vs · Apr 01, 2020 at 07:22 PM
The key_Collect in KeyCheck is starting as true because what you are checking is not whether the player's KeyCollect variable keyCollect is true, but whether or not the player actually has the Component. When booleans are given an object, they become true if the object exists and false if the object is null. Furthermore, a static variable belongs to a class, not the instance, so instead of calling the instance of the player and trying to get keyCollect, you would have to use KeyCollect.keyCollect
.
Hope this helps!
Your answer
Follow this Question
Related Questions
Dragging multiple objects onto a script 1 Answer
damage function not calling 0 Answers
,How to blend Color Curves through script - Color Curve Interpolation 0 Answers
Pressing Button with Vive controller (Collider?) 0 Answers
FPS recoil 1 Answer