photon networking spawning enemies

Options
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
using UnityEngine;
using System.Collections;
 
public class spawnc : MonoBehaviour
{
   
    bool isSpawning = false;
    public float minTime = 5.0f;
    public float maxTime = 15.0f;
    public GameObject[] enemies;
   
    IEnumerator SpawnObject(int index, float seconds)
    {
       
       
        yield return new WaitForSeconds(seconds);
        Instantiate(enemies[index], transform.position, transform.rotation);
         
        isSpawning = false;
    }
   
    void Update ()
    {
        if(! isSpawning)
        {
            isSpawning = true; //Yep, we're going to spawn
            int enemyIndex = Random.Range(0, enemies.Length);
            StartCoroutine(SpawnObject(enemyIndex, Random.Range(minTime, maxTime)));
        }
    }
}

Comments

  • In my test game I have objects spawning.

    This line in your code:
    Instantiate(enemies[index], transform.position, transform.rotation);

    ...should use the PhotonNetwork.Instantiate method. Otherwise it will not get assigned a new PhotonView.

    re:"this is not my code as i did have my spawner in javascript but javascript doesnt seem to work with photon"
    I wrote this current prototype in Javascript using PUN as the starting point.
    http://www.jtadeo.com/photon/snowball1/

    When I have the prototype done, I plan on rewriting it in C#.
  • nice game u have there, played a round of it. il test tht line out later, and thanks soo much i have been stuck on this for such a long time
  • jefftech
    edited November 2014
    Options
    There is an error when you use PhotonNetwork.Instantiate(enemies[index], transform.position, transform.rotation);
    and I think you need to have this function being done by the master client... it should be PhotonNetwork.InstantiateSceneObject, but I can't get that to work either.

    Has anyone figured out how to properly do this, could you paste some example code?
    using UnityEngine;
    using UnityEngine;
    using System.Collections;
    
    public class SpawnEnemy : MonoBehaviour
    {
    	
    	bool isSpawning = false;
    	public float minTime = 5.0f;
    	public float maxTime = 15.0f;
    	public GameObject[] enemies;
    	
    	IEnumerator SpawnObject(int index, float seconds)
    	{
    		
    		
    		yield return new WaitForSeconds(seconds);
    		SpawningEnemies ();
    		
    		isSpawning = false;
    	}
    
    	void SpawningEnemies () {
    		if (!PhotonNetwork.isMasterClient) {
    			PhotonNetwork.InstantiateSceneObject(enemies[index], transform.position, transform.rotation);
    			Debug.LogError("Spawning Enemy");
    		}
    	}
    
    	void Update ()
    	{
    		if(! isSpawning)
    		{
    			isSpawning = true; //Yep, we're going to spawn
    			int enemyIndex = Random.Range(0, enemies.Length);
    			StartCoroutine(SpawnObject(enemyIndex, Random.Range(minTime, maxTime)));
    		}
    	}
    }
    
  • vadim
    Options
    From PhotonNetwork.InstantiateSceneObject documentation: "Only the master client can Instantiate scene objects."
    So checking if(!PhotonNetwork.isMasterClient) is probably wrong.
  • Right... do you know how to correctly make this work? (The other guy has been working on this for 2 months and I've tried different ways to get it to work and I'm not making any better progress.)
  • @jefftech
    I remember there was an example on how to use the PhotonNetwork.InstantiateSceneObject in the PUN download. Check out the examples folder. It had a couple of PDF files that helped to explain how it worked along with scenes.
  • Tobias
    Options
    The Demo Boxes has 2 buttons. One is "Scene" and it allows you to instantiate a scene object - provided you are master client.
    To help you with your issue, we need to know how it fails. Check the console output and if needed, post the error here.