• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by smking204 · Apr 25, 2021 at 10:56 PM · ontriggerenterrpgbooleans

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;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get back player control? 0 Answers

Unity3D: Entering a trigger disables a few bools for no reason 2 Answers

making a variable (var) false when not colliding with a trigger. 1 Answer

Little problem with OnTriggerEnter 1 Answer

OnTrigger, GameObject By Waypoints, GameObject stop till keypress. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges