Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ysadoragm · Aug 31, 2020 at 10:32 PM · errorscript.freezeplaymodeinfinite

Unity freezes on play mode

Hey everyone! I've been trying to build a flocking simulation with fishes and one of my friends helped me to fix part of it and it worked very well on her computer, however, when I tried it on mine it kept freezing everytime I pressed play.

Some people told me it could be an infinite loop, but I'm not sure if that's the problem.

Here is my scripts:

Boid.cs

//using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; using UnityEngine;

public class Boid : MonoBehaviour {

 private static Boid _instance = null;
 private static object _lock = new object();

 public static Boid Instance { 
     get
     {
         lock (_lock)
         {
             if (_instance != null) return _instance;
             Boid boid = FindObjectOfType<Boid>();
             if (boid == null)
             {
                 GameObject go = new GameObject("[Boid]", typeof(Boid));
                 boid = go.GetComponent<Boid>();
             }
             _instance = boid;
         }
         return _instance;
     }
 }

 public GameObject fishPrefab;
 public GameObject goalPrefab;
 public float goalRange = 1f;
 public int tankSize = 10;

 //generate number of fishes
 static int numFish = 10;
  
 private GameObject[] _allFish = new GameObject[numFish];
 public GameObject[] allFish
 {
     get { return _allFish; }
 }

 //public static Vector3 goalPos = Vector3.zero;
 public Vector3 goalPos 
 {
     get 
     {
         if(goalPrefab != null) return goalPrefab.transform.position;
         return Vector3.zero;
     } 
 }

 // Start is called before the first frame update
 void Start()
 {
     //create loop interating through the numFish array
     for (int i = 0; i < numFish; i++)
     {
         //create a postion to our fishes (in a 3Dimensional space)
         allFish[i] = (GameObject)Instantiate(fishPrefab, 
             Random.insideUnitSphere * tankSize, 
             Quaternion.identity);
     }
 }

 // Update is called once per frame
 void Update()
 {
     //Reset where the goal position is randomly 
     foreach(var fish in allFish)
     {
         if((fish.transform.position - goalPos).magnitude <= goalRange)
         {
             goalPrefab.transform.position = Random.insideUnitSphere * tankSize * 0.75f;
             break;
         }
     }
 }

}

Flock.cs

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Flock : MonoBehaviour { public float speed = 1f; float rotationSpeed = 4.0f; Vector3 averageHeading; Vector3 averagePosition; //value of the neighbor distance float neighbourDistance = 3.0f; float minDistance = 1f;

 bool turning = false;

 // Start is called before the first frame update
 void Start()
 {
     //generate a small difference in each neighbor's speed
     speed = Random.Range(0.5f, 1);
 }

 // Update is called once per frame
 void Update()
 {
     if (Vector3.Distance(transform.position, Vector3.zero) >= Boid.Instance.tankSize)
         turning = true;
     else
         turning = false;

     if (turning)
     {
         Vector3 direction = Vector3.zero - transform.position;
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
         speed = Random.Range(0.9f, 1f);
     }
     else
     {
         //flock value
         if (Time.frameCount % 5 == 0)
             ApplyRules(); //function called every 5 times
     }
     transform.Translate(0, 0, Time.deltaTime * speed);
 }

 void ApplyRules()
 {
     GameObject[] allFish = Boid.Instance.allFish;

     // Cohesion of boids
     Vector3 vcentre = Vector3.zero; //center of group
     Vector3 vavoid = Vector3.zero; //points away from any neighbors
     //group speed
     float gSpeed = 0.1f;

     //goal position
     Vector3 goalPos = Boid.Instance.goalPos;

     float dist; //distance variable

     //calculate group size
     int groupSize = 0;
     foreach (GameObject fish in allFish)
     {
         if (fish != this.gameObject)
         {
             dist = Vector3.Distance(fish.transform.position, this.transform.position);
             if (dist <= neighbourDistance)
             {
                 vcentre += fish.transform.position;
                 groupSize++;

                 if (dist < minDistance)
                 {
                     vavoid = vavoid + (this.transform.position - fish.transform.position);
                 }
                 //average speed of group by adding the speed of the flock
                 Flock anotherFlock = fish.GetComponent<Flock>();
                 gSpeed = gSpeed + anotherFlock.speed;
             }
         }
     }

     if (groupSize > 0)
     {
         //calculate average centre and average speed
         vcentre = vcentre / (float)groupSize + (goalPos - this.transform.position);
         speed = gSpeed / (float)groupSize;

         Vector3 direction = (vcentre + vavoid) - transform.position;
         if (direction != Vector3.zero)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
         }
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

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

Terrain tools error, can anyone help? 0 Answers

How to check that only one key is pressed 2 Answers

Combine Mesh Instance is null/Editor freezing when using SkinnedCloth component 0 Answers

unity 3d freezes 5 Answers

I am having an error in my 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