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 lucas216erickson · Dec 31, 2016 at 03:29 PM · photoncrashingfor-loop

For Loop Freezing Client (May be a Photon-related issue)

I have a for loop to spawn X large asteroids and Y small asteroids, and I use a for loop to spawn until I have reached the limit of asteroids. The code is kind of weird, because I needed the asteroids to be synchronized but without photon views, and so I had to have the master client RPC for asteroids to spawn on everyone's clients. I also have a gameobject called "Offline" with a bool called "OfflineMode" so that when I check it things won't be instantiated with photon. On offline mode nothing freezes, but when it is instantiating things over the network the client freezes and crashes. I can't figure out which part of the for loop is doing it, or whether there are too many RPC calls and that's what's killing my computer. using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class SectorManager : MonoBehaviour {
 
     public List <GameObject> LargeAsteroids;
     public List <GameObject> SmallAsteroids;
     public GameObject MainAsteroid;
     public GameObject MiscAsteroid;
     public int LargeAsteroidNumber;
     public int LargeAsteroidOffset;
     public int SmallAsteroidNumber;
     public int SmallAsteroidOffset;
     public PhotonView ThisPhotonView;
     public Manager ManagerScript;
     Vector3 RealPosition;
     Quaternion RealRotation;
     public float SpawnDelay;
     public float DesiredTime;
 
     // Use this for initialization
     void Start () {
         ThisPhotonView = GetComponent<PhotonView> ();
         ManagerScript = GameObject.Find ("Manager").GetComponent<Manager> ();
         DesiredTime = Time.deltaTime + SpawnDelay;
         SetupSector ();
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     public void SetupSector()
     {
         if (!GameObject.Find ("Offline").GetComponent<OfflineMode> ().Offline) 
         {
             if (!PhotonNetwork.isMasterClient) 
             {
                 return;
             }
         }
         for(int A = 0; A < LargeAsteroidNumber; A ++)
         {
             Vector3 SpawnRotation = new Vector3 (Random.Range (2, 358), Random.Range (2, 358), Random.Range (2, 358));
             Vector3 SpawnPos = new Vector3 (Random.Range (transform.position.x - LargeAsteroidOffset, transform.position.x + LargeAsteroidOffset), Random.Range (transform.position.y - LargeAsteroidOffset, transform.position.y + LargeAsteroidOffset), Random.Range (transform.position.z - LargeAsteroidOffset, transform.position.z + LargeAsteroidOffset));
             if (!GameObject.Find ("Offline").GetComponent<OfflineMode> ().Offline) 
             {
                 ThisPhotonView.RPC ("SpawnLargeAsteroid", PhotonTargets.AllBuffered, SpawnPos, SpawnRotation);
             }
             else
             {
                 GameObject CurrentAsteroid = (GameObject)Instantiate (MainAsteroid, SpawnPos, Quaternion.Euler (SpawnRotation));
                 CurrentAsteroid.transform.parent = this.transform;
                 LargeAsteroids.Add (CurrentAsteroid);
             }
         }
     }
     [PunRPC]
     public void SpawnLargeAsteroid(Vector3 Position, Vector3 Rotation)
     {
         GameObject CurrentAsteroid = (GameObject)Instantiate(MainAsteroid, Position, Quaternion.Euler(Rotation));
         CurrentAsteroid.transform.parent = this.transform;
         LargeAsteroids.Add (CurrentAsteroid);
         for (int SA = 0; SA < SmallAsteroids; SA++)
             //SA is short for small asteroid.
         {
             Vector3 SpawnRotation = new Vector3 (Random.Range (2, 358), Random.Range (2, 358), Random.Range (2, 358));
             Vector3 SpawnPos = new Vector3 (Random.Range (CurrentAsteroid.transform.position.x - SmallAsteroidOffset, CurrentAsteroid.transform.position.x + SmallAsteroidOffset), Random.Range (CurrentAsteroid.transform.position.y - SmallAsteroidOffset, CurrentAsteroid.transform.position.y + SmallAsteroidOffset), Random.Range (CurrentAsteroid.transform.position.z - SmallAsteroidOffset, CurrentAsteroid.transform.position.z + SmallAsteroidOffset));
             ThisPhotonView.RPC ("SpawnSmallAsteroid", PhotonTargets.All, SpawnPos, Quaternion.Euler (SpawnRotation));
         }
     }
     [PunRPC]
     public void SpawnSmallAsteroid(Vector3 Position, Vector3 Rotation)
     {
         GameObject CurrentAsteroid = (GameObject)Instantiate(MiscAsteroid, Position, Quaternion.Euler(Rotation));
         SmallAsteroids.Add (CurrentAsteroid);
     }
 
     void OnPhotonSerializeView(PhotonStream Stream)
     {
         if (PhotonNetwork.isMasterClient)
         {
             foreach (GameObject Asteroid in LargeAsteroids) 
             {
                 Stream.SendNext (Asteroid.transform.position);
                 Stream.SendNext (Asteroid.transform.rotation);
             }
             foreach (GameObject Asteroid in SmallAsteroids) 
             {
                 Stream.SendNext (Asteroid.transform.position);
                 Stream.SendNext (Asteroid.transform.rotation);
             }
         } 
         else
         {
             foreach (GameObject Asteroid in LargeAsteroids) 
             {
                 RealPosition = (Vector3)Stream.ReceiveNext ();
                 RealRotation = (Quaternion)Stream.ReceiveNext ();
                 Asteroid.transform.position = Vector3.Slerp (Asteroid.transform.position, RealPosition, 0.1f);
                 Asteroid.transform.rotation = Quaternion.Slerp (Asteroid.transform.rotation, RealRotation, 0.1f);
             }
             foreach (GameObject Asteroid in SmallAsteroids) 
             {
                 RealPosition = (Vector3)Stream.ReceiveNext ();
                 RealRotation = (Quaternion)Stream.ReceiveNext ();
                 Asteroid.transform.position = Vector3.Slerp (Asteroid.transform.position, RealPosition, 0.1f);
                 Asteroid.transform.rotation = Quaternion.Slerp (Asteroid.transform.rotation, RealRotation, 0.1f);
             }
         }
     }
 }
 
Comment
Add comment · Show 1
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 ChristianSimon · Jan 03, 2017 at 09:59 AM 0
Share

Hi,

I'm currently not sure if you reached any limit of RPC calls but you are calling RPCs from inside other RPCs which might be a bad idea. Currently your $$anonymous$$asterClient calls a RPC to create a large asteroid on all clients and himself. $$anonymous$$ight be okay so far. But inside SpawnLargeAsteroid any client who executes this function runs through a for-loop and a calls SpawnSmallAsteroid multiple times via RPC. $$anonymous$$eans that one large asteroid creates I guess too many small asteroids since every client creates additional asteroids.

An approach to solve this is to let the $$anonymous$$asterClient create all spawn positions and their matching rotation data and then send this data bundled to other clients using e.g. RaiseEvent function.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My unity STOPS RESPONDING when this function is activated 1 Answer

Unity crashing when entering for loop 2 Answers

Level genration loop crashing unity3D 2 Answers

Ai script(rotation) causes game to crash 1 Answer

Editor Fails to Re-Launch After Manually Killing The Task Due to Freezing 0 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