Search Unity

[SOLVED] Everybody spawn stuff for themselves

Discussion in 'Multiplayer' started by Gunging, Dec 21, 2016.

  1. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    In my 2D game, random asteroids spawn in the X axis and travel down from top to bottom of the screen, and as its random and spawn like 3 a second, different sizes and all I had not noticed the difference, until now. Apparently NetworkServer.Spawn() is not working?, Whatever it is, everybody spawns their own asteroids, in different positions and sizes, and this is not what I was trying to do, everybody should have the same exact asteroids.

    This is the off-screen gameObject that spawns the meteorites:
    Components:


    Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class MeteorMinigameMechanics : NetworkBehaviour //Its one of three minigames
    6. {
    7.     [SyncVar]private int nextMeteorPosX;     //Explained by itself
    8.     public GameObject lilMeteorPrefab;       //small meteor prefab
    9.     public GameObject meteorPrefab;        //medium meteor prefab
    10.     public GameObject largeMeteorPrefab;    //large meteor prefab
    11.     private float meteorSpawnDelay;        //Absolute time
    12.     public float meteorSpawnRate;            //Relative time
    13.     public bool end;                //explained by itself
    14.     private float timeLeft;               //Explained by Itself
    15.     public GameObject timer;            //Timer GameObject
    16.     [SyncVar]private float nextMeteorSize;    //Explained by Itself
    17.     private float endDelay;            //Absolute time
    18.  
    19.  
    20.     void Start() {
    21.      endDelay = Time.time + 63; //One minute plus 3 seconds
    22.         timer = GameObject.FindWithTag("Timer");    //Explained by Itself
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         timeLeft = timer.gameObject.GetComponent<TimerScript>().TimeLeft; //Gets the time left from the timer's script. (The game starts with 60 seconds, ends at -3 seconds cause end animations)
    28.         if (Time.time > meteorSpawnDelay)    //Explained by Itself
    29.         {
    30.          CmdRandomizeMeteor();     //This one is explained later
    31.             meteorSpawnDelay = Time.time + meteorSpawnRate;    //Makes the absolute delay equal to the current time plus the relative delay (0.3 seconds)
    32.             CmdMeteorSpawn();
    33.         }
    34.         if (timeLeft <= 0 && end == false) { end = true; } //Explained by Itself, triggers end animations.
    35.  
    36.         if (Time.time >= endDelay) { //Explained by Itself, after 3 seconds of end animations changes scene depending if player is still alive (when player is hit, it is destroyed after 1 second of death animation)
    37.             GameObject player = GameObject.FindWithTag("Player"); //Explained by Itself
    38.             if (player == null) { Application.LoadLevel("Looose"); } //Explained by Itself
    39.             if (player.GetComponent<Movement>().died == false) { Application.LoadLevel("Win"); } //Explained by Itself
    40.         }
    41.     }
    42.  
    43.     [Command] void CmdRandomizeMeteor()
    44.     {
    45.         nextMeteorSize = Random.Range(1, 10); //Explained by Itself
    46.      //It is here cause its a SyncVar and needs to be updated in Command
    47.         nextMeteorPosX = Random.Range(-9, 10); //Explained by Itself
    48.      //Same as the one above
    49.     }
    50.  
    51.     [Command] void CmdMeteorSpawn()
    52.     {
    53.         if (nextMeteorSize >= 1 && nextMeteorSize < 6) { //Probability of small meteor is 5/9
    54.             GameObject meteor = (GameObject)Instantiate(lilMeteorPrefab, new Vector3(nextMeteorPosX, 7, 0), Quaternion.identity);
    55.             NetworkServer.Spawn(meteor);
    56.         }
    57.         if (nextMeteorSize >= 6 && nextMeteorSize < 9) { //Probability of medium meteor is 3/9
    58.             GameObject meteor = (GameObject)Instantiate(meteorPrefab, new Vector3(nextMeteorPosX, 7, 0), Quaternion.identity);
    59.             NetworkServer.Spawn(meteor);
    60.         }
    61.         if (nextMeteorSize == 9) //Probability of Big Meteor is 1/9
    62.         {
    63.             GameObject meteor = (GameObject)Instantiate(largeMeteorPrefab, new Vector3(nextMeteorPosX, 7, 0), Quaternion.identity);
    64.             NetworkServer.Spawn(meteor);
    65.         }
    66.     }
    67. }
    68.  

    This is the meteor's stuff, they all have the same thing, just that the scale in the transform variates.
    Components:


    Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Meteor : NetworkBehaviour {
    6.     public float movementSpeed;        //Explained by Itself, (Is set to 3)
    7.     private float damageResistance;    //Life of the meteor, cause of the hits it takes to be destroyed
    8.     public float meteorSize; //1 = Small, 2 = Medium, 3 = Large; Cause Large meteorites are only destroyable by bombs, while small ones by everything.
    9.     public GameObject specialLilMeteorPrefab;    //Some other meteorite spawned by large ones when destroyed
    10.     private float startinSpeed;        //Explained by Itself
    11.     public GameObject meteorStonePrefab;//Some stone they spawn when destroyed and damaged to notice the player his attack succeded and is a special effect. looks rad.
    12.     MeteorMinigameMechanics meteorMinigameExecutor; //The other gameObject's Script
    13.     public bool special;            //For the special meteorites
    14.  
    15.  
    16.     void Start () {
    17.         meteorMinigameExecutor = GameObject.FindWithTag("Executor").GetComponent<MeteorMinigameMechanics>();    //Explained by Itself
    18.         damageResistance = 3;    //Explained by Itself
    19.     }
    20.  
    21.     void Update () {
    22.         GetComponent<Rigidbody2D>().velocity = new Vector2(0, -movementSpeed);    //Explained by Itself, cause it descends.
    23.             if (damageResistance == 0) //When the meteor is destroyed
    24.         {
    25.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    26.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    27.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    28.          //These are only visual, so I dont care players having meteor stones floating in different places.
    29.             if (meteorSize < 3)
    30.             {
    31.                 Destroy(gameObject); //It just destroys if is small or medium
    32.             }
    33.             if (meteorSize == 3)
    34.             {
    35.            CmdSpecialMeteor(); //If its a big one, is destroyed and also spawns some smaller meteors
    36.                 Destroy(gameObject); //Explained by Itself
    37.             }
    38.         }
    39.             if (damageResistance == 3) { startinSpeed = movementSpeed; }  //These 3 make the meteor
    40.             if (damageResistance == 2) { movementSpeed = startinSpeed - 1; }//Travel slower as it
    41.             if (damageResistance == 1) { movementSpeed = startinSpeed - 2; }//Takes damage
    42.          
    43.             if (meteorMinigameExecutor.GetComponent<MeteorMinigameMechanics>().end == true) { Obliterate(); } //If the end (End animation start end, not the real end) happens runs this
    44.     }
    45.     void Obliterate()
    46.     {
    47.         Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    48.         Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    49.         Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    50.         Destroy(gameObject);
    51.      //Is destroyed and spawns 3 meteor stones, these dont harm the player, and fly off the screen.
    52.     }
    53.  
    54.     void OnCollisionEnter2D (Collision2D coll)
    55.     {
    56.         if (coll.gameObject.tag == "Barrier")
    57.         {
    58.             Destroy(gameObject); //Cause of the prefab spamm, I ahve a Barrier at the bottom of the screen destroying the meteors.
    59.         }
    60.         if (meteorSize == 1 && coll.gameObject.tag == "SimpleProjectile" || coll.gameObject.tag == "Explode")
    61.         {
    62.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    63.             damageResistance = damageResistance - 1;
    64.          //Takes damage of projectiles when small
    65.         }
    66.         if (meteorSize != 3 && coll.gameObject.tag == "LargeExplode")
    67.         {
    68.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    69.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    70.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    71.             Destroy(gameObject);
    72.          //If its small or medium gets destroyed isntantly by bomb's explosion
    73.         }
    74.         if (meteorSize == 3 && coll.gameObject.tag == "LargeExplode")
    75.         {
    76.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    77.             damageResistance = damageResistance - 1;
    78.          //If is big bomb explosions just damage it
    79.         }
    80.         if (meteorSize == 2 && coll.gameObject.tag == "Explode")
    81.         {
    82.             Instantiate(meteorStonePrefab, transform.position, Quaternion.identity);
    83.             damageResistance = damageResistance - 1;
    84.          //If is medium only small explosions (not bomb's) damage it.
    85.         }
    86.     }
    87.  
    88.     [Command] void CmdSpecialMeteor(){
    89.           GameObject specialMeteor = (GameObject)Instantiate(specialLilMeteorPrefab, transform.position, Quaternion.identity);
    90.        NetworkServer.Spawn(specialMeteor);
    91.           GameObject specialMeteor2 = (GameObject)Instantiate(specialLilMeteorPrefab, transform.position, Quaternion.identity);
    92.        NetworkServer.Spawn(specialMeteor2);
    93.     }
    94. }
    95.  

    Here is a pic of two games connected to each other, the client (Editor) is running two seconds behind cause An error "Could not spawn object" spamms it. ¿?

    You can see that they are not the same ones, not cause its running 2 seconds behind, cause they dont spawn the same.

    Thanks in advance
     
    Last edited: Dec 21, 2016
unityunity