Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by JohnDevince · Jan 30, 2017 at 07:43 AM · unity 5triggerbooleanif-statements

Bool works everywhere but specific if statement.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using System.Linq;
     
     public class CardMechanics : MonoBehaviour
     {
         public StatusBars statusBars;
         GameObject ZombieCard1, ZombieCard2, ZombieCard3, ZombieCard4, ZombieCard5;
         int randomnumberpicker;
         public bool leftChoice, rightChoice;
         float distance = 10;
     
         void Start()
         {
             statusBars = FindObjectOfType<StatusBars>();
             leftChoice = false;
             rightChoice = false;
         }
     
     
         //Wouldn't ZombieCards() get called before you have a chance to trigger OnMouseDown(), 
         //causing it to instantiate a card (which I presume would create an object with tag "Card") and 
         //result in it never entering that initial if statement in ZombieCards() again?
         void Update()
         {
             if (GameObject.FindGameObjectWithTag("Card") == null)
             {
                 SpawnZombieCard();
             }
             if (leftChoice == true) Debug.Log("Is true");
     
         }
     
         void SpawnZombieCard()
         {
             randomnumberpicker = Random.Range(0, 6);
             switch (randomnumberpicker)
             {
                 case 0:
                     {
                         ZombieCard1 = Instantiate(Resources.Load("Frontcard1")) as GameObject;
                         ZombieCard1.transform.Translate(0, -1, 0);
                         ZombieCardReactions();
                         break;
                     }
                 case 1:
                     {
                         ZombieCard2 = Instantiate(Resources.Load("Frontcard2")) as GameObject;
                         ZombieCard2.transform.Translate(0, -1, 0);
                         ZombieCardReactions();
                         break;
                     }
                 case 2:
                     {
                         ZombieCard3 = Instantiate(Resources.Load("Frontcard3")) as GameObject;
                         ZombieCard3.transform.Translate(0, -1, 0);
                         ZombieCardReactions();
                         break;
                     }
                 case 3:
                     {
                         ZombieCard4 = Instantiate(Resources.Load("Frontcard4")) as GameObject;
                         ZombieCard4.transform.Translate(0, -1, 0);
                         ZombieCardReactions();
                         break;
                     }
                 case 4:
                     {
                         ZombieCard5 = Instantiate(Resources.Load("Frontcard5")) as GameObject;
                         ZombieCard5.transform.Translate(0, -1, 0);
                         ZombieCardReactions();
                         break;
                     }
             }
         }
         void OnMouseDrag()
         {
             if (gameObject.CompareTag("Card"))
             {
                 Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
                 Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
                 transform.position = objPosition;
             }
         }
         void OnTriggerEnter2D(Collider2D coll)
         {
             if (coll.gameObject.tag == "True")
             {
                 leftChoice = true;
                 Debug.Log("Enter is working.");
             }
         }
         void ZombieCardReactions()
         {
             if (ZombieCard1)
             {
                 Debug.Log(leftChoice);
                 if(leftChoice == true)
                 {
                     Debug.Log("You got it working");
                 }
             }
     
         }
     }
 
 
 **The bool leftChoice, which is changed to true upon the trigger enter doesnt work in the if (leftChoice == true) in ZombieCardReactions();
 It actually doesnt effect the if statement at all, but in update I have a Debug.Log that says the bool is actually changing to true. So what gives? Any help would be much appreciated.
 Asked the question but could not get any advice that would actually help me.**

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 jwulf · Jan 30, 2017 at 09:40 AM 0
Share

Could you check

 void ZombieCardReactions()
 {
     Debug.Log("ZombieCardReactions() is called");
     if (ZombieCard1)
     {
         Debug.Log(leftChoice);
         if(leftChoice == true)
         {
             Debug.Log("You got it working");
         }
     }
     else 
     {
         Debug.Log("The var ZombieCard1 does not evaluate to TRUE");
     }
 }

?

If leftChoice really is true and ZombieCardReactions() really is called, the only reasonable assumption is that the first if-Statement does not evaluate to true.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Koen-Matthijs · Jan 30, 2017 at 09:43 PM

Hi

Evaluating your code, below you'll find a scenario how this would happen:

Method ZombieCardReactions has an additional outer IF statement that checks if variable ZombieCard1 actually points to a GameObject instance. Given the randomness you apply in method SpawnZombieCard() it is fairly possible that variable ZombieCard1 is actually null, in which case method ZombieCardReactions() will not reach the second IF statement at all.

It's not a matter of whether or not the bool is correctly evaluated... the code just doesn't get to that point.

With kind regards,

Koen Matthijs

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

6 People are following this question.

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

Related Questions

How to make an enemy stop and start movement in a tower defence game? 0 Answers

Space Shooter Tutorial 5.3 first Asteroid being destroyed 0 Answers

Checking bool in a If statement 2 Answers

How to activate gameObjects based on a boolean from another scene? 1 Answer

Alternate between two Audio clips on collision 3 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