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 /
  • Help Room /
avatar image
0
Question by toadflaxunf · Jan 04, 2018 at 05:24 PM · triggerscript.variableboolean

Boolean variable won't trigger in other script

I have a variable hitTrigger that is set to false by default, and turns true once my player collides with an object. Here's the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BoxCollide : MonoBehaviour {
 
     public GameObject CubeObj;  // cube object 
     public Camera Stall;        // stall camera
     public GameObject Player;   // our fps controller
     public GameObject Ball;
 
     public AudioListener listener;  // this listener needs to me ON when we deactivate 
                                     // the FPS controller
 
     public bool hitTrigger = false;
 
     void Start() {
         // referenced variables
     }
     
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
 
         CubeObj = GameObject.Find("Stall Collider");
         Player = GameObject.Find("FPSController");
         Ball = GameObject.Find("Sphere");
         // now these objects are inside the variables
 
         if (hit.gameObject.name == "Stall Collider")
         {
             Debug.Log("Walked on!");
              Destroy(CubeObj);  // we destory the box collision cube
              Debug.Log("Removed!");
              Stall.enabled = true;  // enables the stall camera
             
             if (Stall.enabled == true) {
                 Player.SetActive(false);    // when we enable stall, we disable FPS
             }
              Debug.Log("Camera Active!");
 
             listener.enabled = true;    // we switch on the new listener
             playMainMusic.Stop();       // and stop the main music
             Debug.Log("Stopped Main Music");
            
             playStallMusic.Play();      // then play the new music
             Debug.Log("Playing Stall Music");
             hitTrigger = true;             // hitTrigger TURNS TRUE HERE
         }
 
         if (hitTrigger == true) {
             Debug.Log("hitTrigger: true");
             // just to be sure the boolean turns true
         }
     }
 }

I then have another script called fisherTimerv2.cs that I need to do something once hitTrigger has turned true. Here's the fisherTimerv2.cs Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class fisherTimerv2 : MonoBehaviour {
 
     bool countStop = false;
     int counter = 0;
     
     public Spawn spawnNewCharacter;
 
     void Start()
     {
         StartCoroutine(Example());
     }
     
     IEnumerator Spawning()
     {
         if (GameObject.Find("FPSController").GetComponent<BoxCollide>().hitTrigger == true)
         {
             Debug.Log("CUSTOMER SHOULD SPAWN!");
             while (countStop == false) {
 
                 yield return new WaitForSeconds(2);
                 Debug.Log("Fisher Spawned!");
                 counter++;
                 spawnNewCharacter.SpawnCharacter();
 
                 if (counter >= 3){
                     countStop = true;
                 }
 
             }
         }
 
     }
 }

As you can see in the IEnumerator Spawning() function, I have an if statement that checks if hitTrigger turns true, then does whatever afterwards, but for some reason I can't get that check in the Spawning() function to work at all, it just completely ignores it.

Any reason for this?

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 Backside2357 · Jan 04, 2018 at 06:14 PM

You should do more debugging first. To do this look at this line

 if (GameObject.Find("FPSController").GetComponent<BoxCollide>().hitTrigger == true)

and split it up so you can debug every single part of it:

 FPSController FPSC = GameObject.Find("FPSController");
 BoxCollide BOXC = FPSC.GetComponent<BoxCollide>();
 bool hitTrigger = BOXC.hitTrigger;
 
 if (hitTrigger == true)

You can now use Debug.Log to look at individual object properties and check if they are what you expect (is FPSC the right FPSController? Is BOXC the right BoxCollide script?). You should also use Debug.Log calls to check if the Spawning() function is even called and if so, if it is called in the right order. Do also a log output in the BoxCollide function to log to the console what the actual value of hitTrigger is.

Comment
Add comment · Show 1 · 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 toadflaxunf · Jan 04, 2018 at 09:27 PM 0
Share

I think I've found the issue, as soon as I run my game, the function gets called straight away, which means it tries to access the if statement but cannot since hitTrigger is false. Is there anyway I can change the code just so the Spawning() function itself gets called when hitTrigger is true ins$$anonymous$$d?

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

142 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 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

Timer onTriggerEnter doesn't launch 0 Answers

Wanting audio to pause on trigger enter 1 Answer

How to keep a variable from changing? 2 Answers

activate button on triggerenter 0 Answers

How to get a public int equal the same number when added in all object that have the same script? 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