Search Unity

photon networking spawning enemies

Discussion in 'Scripting' started by killeraled, Oct 12, 2014.

  1. killeraled

    killeraled

    Joined:
    May 15, 2014
    Posts:
    3
    hi, in the last few days i have been troubled about how to spawn enemies in photon, i have a working singleplayer version of this but the multiplayer side needs the enemies to spawn with a photon view number but every enemie i spawn using the code below comes out with a photonview number 0, and the "Network.Destroy(gameObject);" cant destroy objects with photonview number 0

    this is not my code as i did have my spawner in javascript but javascript doesnt seem to work with photon
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class spawnc : MonoBehaviour
    5. {
    6.    
    7.     bool isSpawning = false;
    8.     public float minTime = 5.0f;
    9.     public float maxTime = 15.0f;
    10.     public GameObject[] enemies;
    11.    
    12.     IEnumerator SpawnObject(int index, float seconds)
    13.     {
    14.        
    15.        
    16.         yield return new WaitForSeconds(seconds);
    17.         Instantiate(enemies[index], transform.position, transform.rotation);
    18.          
    19.         isSpawning = false;
    20.     }
    21.    
    22.     void Update ()
    23.     {
    24.         if(! isSpawning)
    25.         {
    26.             isSpawning = true; //Yep, we're going to spawn
    27.             int enemyIndex = Random.Range(0, enemies.Length);
    28.             StartCoroutine(SpawnObject(enemyIndex, Random.Range(minTime, maxTime)));
    29.         }
    30.     }
    31. }
     
  2. killeraled

    killeraled

    Joined:
    May 15, 2014
    Posts:
    3
    bump? anyone?
     
  3. treshold

    treshold

    Joined:
    Nov 9, 2013
    Posts:
    225
    I'm just beginner myself but if you don't use PhotonNetwork.Instantiate-method, should'nt you assign that photonview id by yourself in your code?
    example from exitgames -reference:
    http://doc.exitgames.com/en/pun/current/reference/pun-and-un
    check out the "Gain More Control: Manually Instantiate" - it has pretty straightforward explanation.
     
  4. killeraled

    killeraled

    Joined:
    May 15, 2014
    Posts:
    3
    thanks for the quick reply, you saved me alot of time of searching around the api :)
     
  5. Brian-Sinasac

    Brian-Sinasac

    Joined:
    Sep 8, 2012
    Posts:
    97
    Is there a reason you didn't use PhotonNetwork.Instantiate?
     
  6. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    You need to use PhotonNetwork and not Network. Also all AI should be instantiated as a scene object so they dont get destroyed if the master client leaves. And ONLY the master client should take decisions for the AI and then send messages to the other players and let them update the AI accordingly.

    Also when doing AI remember that if you got more than a few AI's you cannot update them in the same manner as with Players as PUN is limited to 500 messages per room.
     
  7. sonnolo

    sonnolo

    Joined:
    Feb 22, 2015
    Posts:
    26
    Hello guys,i ave this problem,ia ve add player spawn zombie when die but just killer player see bot...suggestions?
     

    Attached Files: