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 AnyOtherProgrammer · Jun 12, 2017 at 09:15 AM · errorstartplay mode

My script doesn't let unity start

So, I have a script and when its active in the scene, Unity doesn't enter play mode. It gets stuck trying to enter, and I have to force it to close via task manager. Heres the code:

 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 public class PlaygroundGenerator : MonoBehaviour {
 
     [SerializeField]
     private int players = 4;
     [SerializeField]
     private int stonesPerPlayer = 4;
     [SerializeField]
     private int areaSizeFactor = 4;
 
     void Update ()
     {
         gameObject.GetComponent<MeshFilter>().mesh = GeneratePlaygroundMesh(players,stonesPerPlayer,areaSizeFactor);
     }
 
     public Mesh GeneratePlaygroundMesh(int _players,int _stonesPerPlayer,int _areaSizeFactor)
     {
         int sideSize = 3;
         float angleStep = 360.0f / _players;
         List<Vector3> vertexList = new List<Vector3>();
         List<int> triangleList = new List<int>();
         Quaternion quaternion = Quaternion.Euler(0.0f, angleStep, 0.0f);
         // Make first triangle.
         float radius = sideSize / 2f / Mathf.Cos((90f - angleStep / 2) * Mathf.Deg2Rad);
         vertexList.Add(new Vector3(0.0f, 0.0f, 0.0f));    // 1. Circle center.
         vertexList.Add(new Vector3(0.0f, 0.0f, radius));  // 2. First vertex on circle outline 
         //Debug.Log(radius);
         vertexList.Add(quaternion * vertexList[1]);       // 3. First vertex on circle outline rotated by angle)
                                                           // Add triangle indices.
         triangleList.Add(0);
         triangleList.Add(1);
         triangleList.Add(2);
         for (int i = 0; i < _players - 1; i++)
         {
             triangleList.Add(0);                      // Index of circle center.
             triangleList.Add(vertexList.Count - 1);
             triangleList.Add(vertexList.Count);
             vertexList.Add(quaternion * vertexList[vertexList.Count - 1]);
         }
         for (int i = 0; i < vertexList.Count - 1; i++)
         {
             if(i != 0)
             {
                 //Debug.DrawRay(vertexList[i], vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]);
                 //Debug.DrawRay(vertexList[i], vertexList[i] + (vertexList[(i == 1) ? _players : i - 1] - vertexList[i]) / 2 - vertexList[0]);
 
                 /*vertexList.Add(vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
                 triangleList.Add(i);
                 triangleList.Add(i + 1);
                 triangleList.Add(vertexList.Count - 1);  
                 */
 
                 /*
                 Debug.DrawLine(vertexList[i], vertexList[i + 1]);
                 Debug.DrawLine(vertexList[i + 1], vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
                 Debug.DrawLine(vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor), vertexList[i]);
 
                 Debug.DrawLine(vertexList[i + 1], vertexList[i + 1] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
                 Debug.DrawLine(vertexList[i] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor), vertexList[i + 1] + ((vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized * _areaSizeFactor));
                 */
                 
                 Vector3 normalDir = (vertexList[i] + (vertexList[i + 1] - vertexList[i]) / 2 - vertexList[0]).normalized; //
                                                                                                                           //
                 Vector3 UpperI = vertexList[i] + (normalDir * _areaSizeFactor);                                              //
                 Vector3 UpperIPlus1 = vertexList[i + 1] + (normalDir * _areaSizeFactor);                                  //
                                                                                                                           //
                 vertexList.Add(UpperI);                                                                                      //
                 vertexList.Add(UpperIPlus1);                                                                              // When I comment out  
                                                                                                                           // this area, Unity 
                 int UpperIIndex = vertexList.IndexOf(UpperI);                                                              // can run.
                 int UpperIPlus1Index = vertexList.IndexOf(UpperIPlus1);                                                      //
                                                                                                                           //
                 triangleList.Add(i);                                                                                      //
                 triangleList.Add(UpperIIndex);                                                                              //
                 triangleList.Add(i + 1);                                                                                  //
                                                                                                                           //
                 triangleList.Add(UpperIIndex);                                                                              //
                 triangleList.Add(UpperIPlus1Index);                                                                          //
                 triangleList.Add(i + 1);                                                                                  //
                 
             }
         }
         Mesh mesh = new Mesh();
         mesh.vertices = vertexList.ToArray();
         mesh.triangles = triangleList.ToArray();
         return mesh;
     }
 
     void OnValidate()
     {
         if (players < 3)
         {
             players = 3;
         }
 
         if (stonesPerPlayer < 4)
         {
             stonesPerPlayer = 4;
         }
 
         if (areaSizeFactor < stonesPerPlayer)
         {
             areaSizeFactor = stonesPerPlayer;
         }
     }
 }

So , does this code run on your PC? If so, please tell me. If it doesn't but you understand why, tell me. PS: to run this, just put this code on an empty game object.

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

2 Replies

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

Answer by NoseKills · Jun 12, 2017 at 03:21 PM

You do have this in there

 for (int i = 0; i < vertexList.Count - 1; i++)
 {
     if(i != 0)
     {
         ...
         vertexList.Add(UpperI);
         vertexList.Add(UpperIPlus1);
 

vertexList.Count increases by 2 every loop and i increments by 1... You'll never hit the end condition.

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 AnyOtherProgrammer · Jun 12, 2017 at 05:05 PM 0
Share

Sir, you are my savior and a true genius.

avatar image
0

Answer by gameplay4all · Jun 12, 2017 at 10:05 AM

I think it does run, but terribly slow. I don't see an infinite loop in you code (that's often the problem when the editor gets stuck) so this function is just taking an enormous time to calculate. I haven't had a good look at your code but this mesh doesn't seem to change every frame so why not:

  • move the function call to Start instead of Update

  • Cache the MeshFilter component and remove the GetComponent (slight performance boost).

  • Pass the MeshFilter to the GeneratePlaygroundMesh function and set it's vertices and triangles directly, saves the performance of generating a completely new mesh.

  • If you are continuously updating the mesh (so you keep it in Update) you need to call Mesh.Clear before each change, if you use the above method.

See https://docs.unity3d.com/ScriptReference/Mesh.html for some other tips.

I hope this solved your problem, and sometimes patience is the answer. I had occasions where my start function (before optimization of course) would take 30+ seconds but in the end the game would run.

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 AnyOtherProgrammer · Jun 12, 2017 at 02:03 PM 0
Share

I'm glad for your answer, and I'll try these, but patiece is not the answer(I waited ~1h).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

FindGameObjectWithTag () failing in unity 5.0.2? 1 Answer

monodevelop shows error and does not start 0 Answers

I rendered my game and it crashes when I start it, how can I fix it? 1 Answer

Tons of compiler Errors.. Please help ;_; 1 Answer

Fixed update called before start 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