Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Third Party PhotonNetwork.Instantiate

Discussion in 'Multiplayer' started by Shears, Aug 25, 2015.

  1. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    Im trying to get the enemys to sync on all players screen when they spawn but im coming across an error. I fully understand the error and why it is happening im just not sure how to fix it. I dont like posting my whole script but here it goes

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ZombieSpawn : Photon.MonoBehaviour {
    4.     private float minRepeatRate = 1f;        //Lowest Random Spawn Rate (Keep at 1 for best results)
    5.     private float maxRepeatRate = 1000f;    //Highest Random Spawn Rate (Will increase random spawning time if decreased and will decrease random spawning time if inscreased)
    6.     public float spawnTime = 15;             //Time Before Enemies Spawn
    7.     public float repeatRate;                //
    8.     private float furthestDistance = 0.0f;    //Finds Furthest Distance from player
    9.     private float distance = 0.0f;            //Find all Disrances from player
    10.     public Transform[] SpawnPoints;            //Holds All Spawn Location information
    11.     private Transform furthestSpawnPoint;    //Holds Position of the current Furthest Spawn Location
    12.     private Transform PlayerTransform;        //Holds Players position
    13.     public GameObject[] Zombie;            //Array of Enemies to spawn, Add as many different enemies you want to spawn in the Inspector area.
    14.     void Update ()
    15.     {
    16.         repeatRate = Random.Range (minRepeatRate, maxRepeatRate);    //Randomizes Repeat rates
    17.         if (repeatRate <= 1.74)                                     // Increase This To Spawn Enemys Faster
    18.         {
    19.             Invoke ("SpawnEnemy", spawnTime);                        // Spawn Enemy
    20.         }
    21.     }
    22.     [PunRPC]
    23.     void SpawnEnemy()
    24.     {
    25.         PlayerTransform = GameObject.FindWithTag("Player").transform;     // Gets Current Players Transform
    26.         foreach(Transform point in SpawnPoints)                            // Stores Distances from player to spawnpoints
    27.         {
    28.             distance = Vector3.Distance(PlayerTransform.position, point.position);
    29.             if (distance > furthestDistance)
    30.             {
    31.                 furthestDistance = distance;
    32.                 furthestSpawnPoint = point;
    33.             }
    34.         }
    35.         int enemyIndex = Random.Range (0, Zombie.Length);                // Picks a random Enemy
    36.         PhotonNetwork.Instantiate (Zombie[enemyIndex], furthestSpawnPoint.position, furthestSpawnPoint.rotation, 3); // Spawns a Random Enemy at a Random SpawnSpot
    37.    
    38.     }
    39. }
    The Error I get is:

    Code (CSharp):
    1. /Scripts/ZombieSpawn.cs(45,31): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject' expression to type `string'
    The error is appearing because "Zombie[enemyIndex]" is a Game object. And im guessing it needs to be a string.

    and With how I have THIS particular script where I cannot just use the prefab name for a string.
    because it grabs a random enemy prefab from the enemyIndex line above the instantiate line.

    There must be some type of work around here, unfortunatly I have not learned this yet..

    Any help is appreciated once again. thanks
     
  2. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    bump. Anyone?

    I guess I could just make a seperate script for each enemy prefab, but would like to learn the proper way...
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    If the list contains Prefabs, you can use each item's .name. It should be the same as the prefab itself and it's a string.
     
  4. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    Thanks for the reply, I will try this out right away
     
  5. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    @tobiass
    I got it to work with "Zombie[enemyIndex].name" but the enemys are still not syncing?
    Im trying to get the enemys position / animation to be the same for all players..

    I have a few different scripts for the enemy but Im not sure which one I need to use accross the network:
    ZombieSpawn -- The script above to spawn the enemys
    ZombieSpawnTrigger -- activates "ZombieSpawn" script when player is in range(already activates/deactivates for all players)
    NetworkEnemy -- pretty much just the OnPhotonSerializeView function with position,rotation,animation
     
  6. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    They seem to only spawn with group 0, even though the PhotonView is 4