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 /
avatar image
0
Question by POLYGAMe · Apr 23, 2014 at 04:28 AM · collisionperformanceontriggerenterstutter

OnTriggerEnter Bad Performance - Bad Code?

I have the following script set up to deploy AI cars from a pool of 13. Each trigger has an array of spawn points, which are just empty objects.

Currently I only have one trigger set up and when the player triggers it, there is a very noticeable pause. Is my code that horrific?

As I said, the array of cars is only 13. I don't get why there is this pause because before I was drawing from a pool of 13 inactive cars, I had it hardcoded with over 100 cars in the scene and it was working flawlessly... not sure how this "optimising" managed to lower performance.

I'm not the greatest coder by any means (I'm quite crap, actually), so any insight into what I have done incorrectly would be amazeballs ;)

 #pragma strict
 
 var SpawnerArray : GameObject[]; // Assign Spawners in inspector
 var AICarArray : GameObject[]; // Assign cars in inspector
 
 var iRandom : int = 0;
 var iRandom2 : int = 0; 
 var iIndex : int = 0;
 
 function OnTriggerEnter(Other : Collider)
 {
     if (Other.tag == "Player")
     {
         iRandom = Random.Range(0, SpawnerArray.Length);
     
         for (var i = 0; i < iRandom; ++i)
         {
             iRandom2 = Random.Range(0, AICarArray.Length); 
             iIndex = iRandom2;
             
             if (!AICarArray[iIndex].active)
             {
                 AICarArray[iIndex].transform.position = SpawnerArray[i].transform.position;
                 AICarArray[iIndex].transform.rotation = SpawnerArray[i].transform.rotation;
                 AICarArray[iIndex].SetActive(true);
             }
         }
     }
 }
Comment
Add comment · Show 13
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 getyour411 · Apr 23, 2014 at 04:37 AM 0
Share

This isn't your issue but if you are tweaking, why do this

  iRandom2 = Random.Range(0, AICarArray.Length);
 iIndex = iRandom2;

ins$$anonymous$$d of

 iIndex = Random.Range(...);

since you aren't using iRandom2 anywhere. The time is probably in the SetActive() since that has to run Awake and Start for all the AICarArray[] gameobjects that get selected

avatar image POLYGAMe · Apr 23, 2014 at 04:54 AM 0
Share

Yeah, I was using iIndex as I was reseting it to cycle through if the car was already active. I just forgot to change it back.

I didn't think of it having to run start/awake etc for each car. I'll try setting the cars to active from the beginning and just modify their positions etc. Though before I had 100 or so inactive cars and I was using OnTriggerEnter to set them to active...

avatar image POLYGAMe · Apr 23, 2014 at 04:57 AM 0
Share

Hmm, no it's not that. I disabled the scripts on the cars... still the same issue. It does seem to make a difference as to how many cars are being spawned but I don't see why it should, as it is never more than four.

avatar image getyour411 · Apr 23, 2014 at 05:11 AM 0
Share

There's a subtle difference between ++i and i++ but what it is I don't remember; I usually see that in the form of i++ doubt it makes a difference.

avatar image POLYGAMe · Apr 23, 2014 at 05:13 AM 0
Share

Yeah, it's not that.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by arklay_corp · Apr 23, 2014 at 08:13 AM

It doesn't look wrong, I'd check if the problem is in another script attached to your cars

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 POLYGAMe · Apr 23, 2014 at 08:22 AM 0
Share

Yeah it's odd. It's fine on device too, just doing it in the editor, so I'm not too worried.

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

23 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

Related Questions

[SOLVED] OnTriggerExit only called when object moves 0 Answers

OnTriggerEnter2D on an empty game object with a box collider? 1 Answer

How to Apply a script During collison? 3 Answers

Detect when a trigger has entered a collider 1 Answer

Tree Coliders and Performance 2 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