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
1
Question by manGo29 · Jul 26, 2013 at 02:55 PM · crashloopforloop

Why is this forloop crashing???

I've been stuck here for a day. I'm trying to call a simple return to find out which player on the network is the one performing an action. In this case, a script is highlighting units based on the returned player's alliance status when that unit is Moused-over. The most frustrating part is, SOMETIMES I crash, and other times I don't, so I know I'm doing something really dumb, but I can't figure it out.

Here's the Highlighter snippet:

 void OnMouseEnter()
     {
         int i = 0;
         tGroup = this.transform.GetComponentsInChildren<Transform>();
         int tPlayer = nManager.GetTriggeringPlayer();
         Debug.Log(tPlayer);
         if(tPlayer!=0)
         {
             if(nManager.gControllers[tPlayer].alliances[uData.playerNumber]==GameController.AllianceStatus.allied)
                 hColor = allyColor;
             if(nManager.gControllers[tPlayer].alliances[uData.playerNumber]==GameController.AllianceStatus.enemy)
                 hColor = enemyColor;
             if(nManager.gControllers[tPlayer].alliances[uData.playerNumber]==GameController.AllianceStatus.neutral)
                 hColor = neutColor;
             while(i<tGroup.Length)
             {
                 if(tGroup[i].renderer)
                 {
                     tGroup[i].renderer.material.color = hColor;
                     i++;
                 }
             }
         }
     }


and here's the GetTriggeringPlayer call:

  public int GetTriggeringPlayer()
         {
             GameObject[] gC = GameObject.FindGameObjectsWithTag("GameController");
             for(int pN = 0;pN<=gC.Length;pN++)
             {
                 if (gC[pN].networkView.isMine)
                 {
                     int number = gC[pN].GetComponent<GameController>().PlayerNumber;
                     return number;
                     break;
                 }
             }
             int noNumber = 0;
             return noNumber;
         }
Comment
Add comment · Show 2
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 Linus · Jul 26, 2013 at 03:20 PM 0
Share

What is the exact error message

avatar image manGo29 · Jul 26, 2013 at 03:25 PM 0
Share

I'm not getting an error message, Unity will simply freeze and I have to force it to close completely; common for a loop crash.

If I remove the Highlighter script from the units or comment out the line that calls the GetTriggeringPlayer return it doesn't freeeze, but then obviously nothing is accomplished. So I know its the loop that's causing the problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jul 26, 2013 at 04:06 PM

That's pretty simple. You only increment "i" when the current object has a renderer component. If it doesn't you'll be stuck in your loop since I won't change anymore.

Move the i++ out of you if statement at the end of your while body.

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 manGo29 · Jul 26, 2013 at 04:08 PM 0
Share

AHH I knew it had to be something stupid. I my other solution I completely got rid of that loop, but you're right, I'm sure that was the loop that was wrecking me. Big thanks!

avatar image
0

Answer by tw1st3d · Jul 26, 2013 at 02:57 PM

Try defining it as

 public GameObject[] gC = GameObject.FindGameObjectsWithTag("GameController");

outside of your methods at the start of the class. Only thing I can think of is it's not enumerating the list fast enough and your for() is crashing because it's finding empty fields.

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 manGo29 · Jul 26, 2013 at 03:26 PM 0
Share

I tried moving this, and I still froze, but that was a good suggestion and is leading my to try some different things. I'll keep you updated.

avatar image manGo29 · Jul 26, 2013 at 04:07 PM 0
Share

Well I think I've solved this for now, after your suggestion got me thinking that it wasn't finding fields fast enough. I ended up setting a sort of master local player variable in my network controller, which the highlighter script is now reading ins$$anonymous$$d of calling a GetTriggeringPlayer return. For now everything looks good for the network controller, I hope it doesn't wreck local players in the future.

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

18 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

Related Questions

Loop to populate array crashes Unity 1 Answer

Crash on loop using Lerp. 1 Answer

Is this loop infinite? 2 Answers

Unity crashes when doing A* pathfinding loop 1 Answer

Infinite Looping Crash 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