Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Oct 30, 2013 at 11:24 AM by WhipJr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by WhipJr · Oct 30, 2013 at 11:13 AM · c#bool

Why is my variable always true?

This script is a random encounter script I've been working on, everything is working as i expected aside from one slight problem. My "InBattle" bool is always true and i can't understand why. I only have one spot where it is even allowed to return true, but that is after certain numerical conditions are met. Maybe someone on here could shine some light on this irritation. (:

 using System.Collections;
 
 public class BattleSystem : MonoBehaviour {
     
 public  bool InBattle = false;    
     
 private int RandNum;
 public int RangeStart = 1;
 public int RangeEnd = 4000;
 private bool Encounter = false;
 public int EncounterRangeStart = 400;    
 public int EncounterRangeEnd = 600;    
     
 public float SecondsWalking;
 public float StartWalking;
     
     //for player location/rotation on load
 public string AfterBattleMap;     
 public Vector3 AfterBattleLocation;
 public Quaternion AfterBattleRotation;    
  
 
 void Awake () {
         
           Application.targetFrameRate = 30;
     }    
     
     // Use this for initialization
 void Start () {
  
 }
  
 // Update is called once per frame
     //This is used to initiate the random battles.
 void Update () {
  
         //initialize RandNum with a random number 1-1000
     RandNum = Random.Range(RangeStart, RangeEnd);
         
         //keep track of when the player started walking
     if(Input.GetKeyDown(KeyCode.W)||Input.GetKeyDown (KeyCode.S)||Input.GetKeyDown(KeyCode.A)||Input.GetKeyDown (KeyCode.D)){
         
         StartWalking = Time.time;    
             
         }
         //add the difference of when the player started walking and when the player stopped walking
     if(Input.GetKeyUp(KeyCode.W)||Input.GetKeyUp (KeyCode.S)||Input.GetKeyUp(KeyCode.A)||Input.GetKeyUp (KeyCode.D)){
             
         SecondsWalking+=Time.time-StartWalking;    
             
         }
     if(InBattle=false){    
     //get forward and back movement and check for battle conditions, also increment time walkin    
         if (Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.S)){
                 if(Mathf.Round (Time.time)%10==0){ 
                 Encounter = true;
                     
             //Random battle conditions, ir Rand is greater than 4 but less than 10 start a battle,
             //and if RandNum is greater than 400 but less than 600 start a battle.
                     if(Encounter){
                 //Debug.Log("TimeTrue");
                 //reset encounter chance
                         Encounter = false;
                         if(RandNum > EncounterRangeStart && RandNum < EncounterRangeEnd){  
                             InBattle = true;
                             StartBattle ();
                             
                             
                         }
                     }    
                 }
             }
         }
         
         if(InBattle=true){
             if(Input.GetKeyDown(KeyCode.Q))
             {
                 EndBattle();
             }
         }
     }
     
     
     
 void StartBattle () {
     if(InBattle){    
         Debug.Log ("StartBattle");
         //caches the map the player was on.
         AfterBattleMap = Application.loadedLevelName;
                         
         //caches the Player's location & rotation.
         AfterBattleLocation = GameObject.FindGameObjectWithTag("Player").transform.position;
         AfterBattleRotation = GameObject.FindGameObjectWithTag("Player").transform.rotation;
                         
            
         if(InBattle){
         Application.LoadLevel("000Creation");
         Debug.Log("Battle!");
         }
         }
         
     }
 void EndBattle()
     {
         Application.LoadLevel(AfterBattleMap);
         Debug.Log ("EndBattle");
                 
                 InBattle=false;
                 if(GameObject.FindGameObjectWithTag("Player")== null)
                 {
                 OnLevelWasLoaded();
                 }
     }
     
 void OnLevelWasLoaded()
     {Debug.Log("OnLevelWasLoaded");
         if(GameObject.FindGameObjectWithTag("Player")!= null)
                 {
                 GameObject.FindGameObjectWithTag("Player").transform.position = AfterBattleLocation;
                 GameObject.FindGameObjectWithTag("Player").transform.rotation = AfterBattleRotation;
                 }
     }    
 }
Comment
Add comment · Show 1
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
avatar image meat5000 ♦ · Oct 30, 2013 at 11:18 AM 0
Share
 if(InBattle=true)

==

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by tanoshimi · Oct 30, 2013 at 11:18 AM

if(InBattle=false) needs to be if(InBattle==false) (or, if you prefer, if(!InBattle))

if(InBattle=true) needs to be if(InBattle==true) (or, if you prefer, if(InBattle))

Comment
Add comment · Show 2 · 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
avatar image WhipJr · Oct 30, 2013 at 11:23 AM 0
Share

thank you, I knew it had to be something easy... i've been looking at this too much this morning :p

avatar image Hoeloe · Oct 30, 2013 at 12:42 PM 0
Share

Just remember that = is the assignment operator, while == is the equality comparison operator. It's worth remembering the difference for future use - they are two completely distinct operators.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Script won't reactivate after I deactivate it through a script 1 Answer

Booleans are not working 1 Answer

Quick question about arrays and MonoDevelop 1 Answer


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