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 bspurewal · May 27, 2020 at 09:21 PM · instantiatecollider2dif statementfunction call

Why is a function being called for one trigger but not the other?

I am working on a top-down infinite scroller where the player drives up and avoids traffic that is spawned on random locations on the road. The player has a box collider (tagged "PlayerCheck") that prevents traffic from spawning within view of the player camera. Each instantiated prefab of traffic has a capsule collider (tagged "Traffic") that is intended to prevent traffic from spawning on top of existing traffic.


The road has an array of spawn points. The road also has a box collider that sits above the spawn points. The road has a spawn manager script (see below). The trigger colliders determine the values of the two boolean variables. If those values are both true, a spawn function is called (NorthSpawnMechanism).


The main issue is that for some reason, the PlayerCheck collider successfully turns off the spawn function, but the Traffic collider does not. The Traffic collider even correctly prints the Traffic Debug.Log messages when Traffic passes through the road spawn point collider. Why is one of my trigger colliders (PlayerCheck) preventing the call of a function while the other (Traffic) does not?


I have tried removing the PlayerCheck and tested the Traffic trigger by itself and still no luck. Thanks in advance for any assistance (first time coder here)!


     bool spawnCarsPlayer = true;
     bool spawnCarsTraffic = true;
 
     void Update()
     {
         if (spawnCarsPlayer == true && spawnCarsTraffic == true)
         {
             NorthSpawnMechanism();
         }
     }
 
     void NorthSpawnMechanism()
     {
         if (Time.time > nextSpawnTime)
         {
             nextSpawnTime = Time.time + secondsBetweenSpawns;
             randomNorthSpawnPoint = Random.Range(0, northSpawnPoints.Length);
             Instantiate(NorthTrafficPrefab, northSpawnPoints[randomNorthSpawnPoint].position, Quaternion.identity);
         }
     }
 
 void OnTriggerEnter2D(Collider2D other)
     {
         
         if (other.tag == "PlayerCheck")
         {
             spawnCarsPlayer = false;
             Debug.Log("The player can see the spawn point.");
         }
         
 
         if (other.tag == "Traffic")
         {
             spawnCarsTraffic = false;
             Debug.Log("There is traffc in the way.");
 
         }
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {
 
         if (other.tag == "PlayerCheck")
         {
             spawnCarsPlayer = true;
             Debug.Log("The player has moved on.");
 
         }
 
         if (other.tag == "Traffic")
         {
             spawnCarsTraffic = true;
             Debug.Log("The traffic has moved on.");
 
         }
 
     }


Comment
Add comment · Show 3
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 unity_ek98vnTRplGj8Q · May 27, 2020 at 10:28 PM 0
Share

Nothing immediately jumps out as wrong... Is there only a single spawner object? Is there only a single spawner collider? Is a single instance of traffic meant to block all spawning objects until it has moved far enough away?

avatar image bspurewal unity_ek98vnTRplGj8Q · May 27, 2020 at 10:37 PM 0
Share

Hi, thanks for the response!

No, there are several spawning objects. There are 4 sections of 'road' at all times, and each road section has 3 spawn points and a spawn manager script.

avatar image bspurewal unity_ek98vnTRplGj8Q · May 27, 2020 at 10:38 PM 0
Share

And each road has one spawner collider. No, there are several instances of traffic and each traffic prefab should turn the boolean false while colliding with the spawner collider.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Map-Builder · May 29, 2020 at 12:09 AM

Too long question, I'm not sure what happens in your scene, how people could answer ? There are so many questions...


But just in case OnTriggerEnter is called when collision enters a trigger, I did have issue with thngs being already in...

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
avatar image
0

Answer by unity_ek98vnTRplGj8Q · May 27, 2020 at 10:45 PM

Ok, and I assume traffic spawned by one spawner can cross over into another spawner's collider space? If so then the first thing I would assume is going wrong is that if two objects are in the spawning trigger and then one leaves it will still set spawnCarsTraffic to true, even if the other object is still in the trigger area. To fix this you can make spawnCarsTraffic an int, and add 1 to it on Trigger Enter and subtract 1 on Trigger Exit, and just check if it is equal to zero before you spawn anything. If this still doesn't fix the problem then I recommend making the spawnCarsTraffic variable public so you can watch it in the inspector while your game is running and see whether it never works, or only sometimes doesn't work, or perhaps gets changed back immediately every time it is set to true (or gets incremented if you change to an int)

Comment
Add comment · Show 4 · 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 bspurewal · May 27, 2020 at 10:48 PM 0
Share

Yes, the traffic spawned by one road does cross into the next road. Okay, that makes sense I'll give it a shot, thank you so much!!

And thank you for the tip on making the variable public, I was worried my Debug check wasn't returning the information I needed to verify the boolean.

BP

avatar image unity_ek98vnTRplGj8Q bspurewal · May 27, 2020 at 10:53 PM 0
Share

Great, let me know what you find

avatar image bspurewal · May 28, 2020 at 06:28 PM 0
Share

It's working as intended! Thank you!!

avatar image mucilagiuww · Jun 01, 2020 at 09:35 AM 0
Share

Superb article and I would really like to thank for your article it’s really helpful. tellthebell

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

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

detect if collider is not touching anything 0 Answers

Ignore collision before instantiating 1 Answer

How to instantiate prefabs at random times within a time constraint? 2 Answers

how to prevent an gameobject of instantiating if theres an collider where it is supposed to collide 1 Answer

Instantiate particles with double if statement 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