您好,我正在制作一个多人射击在unity和即时通讯使用数组和需要帮助
我的情况是,我想做一个拆箱系统,选择一个随机数组,然后将它设置为classes数组列表的列表,请帮助。请告诉我这是否会选择一个随机数组: ClassesRandom;我还需要知道如何从不同的脚本设置数组
using UnityEngine;
using System.Collections;
public class SpawnNew : MonoBehaviour
{
public GameObject[] Classes;
public GameObject player;
public Transform[] spawnPoints;
public Camera spawnCam;
public AudioListener spawnListener;
public bool spawned = false;
void ClassList(){
Classes = new GameObject[10];
}
void OnGUI()
{
if (Network.isServer || Network.isClient)
{
if (!spawned)
{
if (GUI.Button(new Rect(Screen.width / 10 - 50, Screen.height / 10 - 25, 100, 50), "Spawn"))
{
SpawnPlayer();
}
}
}
}
void SpawnPlayer()
{
spawned = true;
int random = Random.Range(0, spawnPoints.Length);
Network.Instantiate(player, spawnPoints[random].position, spawnPoints[random].rotation, 0);
spawned = true;
spawnCam.enabled = false;
spawnListener.enabled = false;
spawned = true;
}
public void Die()
{
Screen.lockCursor = false;
spawned = false;
spawnCam.enabled = true;
spawnListener.enabled = true;
}
}发布于 2016-05-03 14:07:18
首先,你的数组没有初始化。您必须确保在调用SpawnPlayer方法之前填充了数组,否则将通过NullReferenceException进行填充。
是的,这个int random = Random.Range(0, yourArray.Length);会给你数组中的一个随机索引。您可以从另一个脚本中访问数组/方法,如下所示
GameObject yourObject = GameObject.Find("GameObjectNameThatHasYourScriptAttachedToIt");
if(yourObject !=null){
yourObject.GetComponent<SpawnNew>().Classes = someOtherArray;
// or if you want to access the method
// yourObject.GetComponent<SpawnNew>().SpawnPlayer();
}https://stackoverflow.com/questions/36995215
复制相似问题