First, I created a list that filters objects.
To make it clearer, I will describe what the player is doing. The player comes to the rack with a book. The camera is approaching the table (done), and after n seconds, characters from the list appear, in an amount of 3 pieces. You can click on each of the characters, and then his biography will appear. After that, you can kill one of the characters. Then all 3 are removed and 3 new characters are created. But do not create those that were already on the table.
public void characterFilter() { List<CharactersDie> FilteredData = new List<CharactersDie> (); foreach (CharactersDie C in characters) { // если объект еще не создавался, то это значение равно 0 // если создавался, то оно равно 1 if (C.characterReads == 0) FilteredData.Add (C); } Как из этого списка мне создать 3 рандомных и не повторяющихся объекта? If it helps, here is the complete code.
using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.FirstPerson; using System.Collections.Generic; public class LevelManagerScript: MonoBehaviour { public UnityStandardAssets.Characters.FirstPerson.FirstPersonController controller; private Books playerReadings; private RayCastScript rayCast; public GameObject openBook; private OpenBook openBooks; private CharactersDie[] characters; public GameObject deathBookCamera; public GameObject characterControll; public float loadDelay; // Use this for initialization void Awake() { controller = FindObjectOfType<FirstPersonController>(); playerReadings = FindObjectOfType<Books>(); rayCast = FindObjectOfType<RayCastScript>(); Cursor.visible = false; characters = FindObjectsOfType<CharactersDie>(); //openBook = GameObject.FindGameObjectWithTag ("OpenBook"); } void Start() { } // Update is called once per frame void Update() { //if (playerReadings.playerReading) { // Cursor.visible = true; //} else // Cursor.visible = false; } public void reading() { controller.enabled = false; rayCast.enabled = false; Cursor.visible = true; } public void StopReading() { openBooks = FindObjectOfType<OpenBook>(); rayCast.enabled = true; //openBook.TextCanvas.SetActive(false); openBooks.OpenBookTextCanvas.SetActive(false); openBooks.oneText.SetActive(false); openBooks.twoText.SetActive(false); openBooks.threeText.SetActive(false); openBooks.fourText.SetActive(false); playerReadings.playerReading = false; controller.enabled = true; Cursor.visible = false; } public void characterFilter() { List <CharactersDie> FilteredData = new List<CharactersDie>(); foreach(CharactersDie C in characters) { if (C.characterReads == 0) FilteredData.Add(C); } foreach(CharactersDie C in FilteredData) { int prefabIndex = UnityEngine.Random.Range(0, C); // это не работает Instantiate(FilteredData[prefabIndex]); } } public void deathBookCameraActivate() { characterControll.SetActive(false); deathBookCamera.SetActive(true); Cursor.visible = true; } public void deathBookCameraDisActivate() { characterControll.SetActive(true); deathBookCamera.SetActive(false); Cursor.visible = false; } }
// это не работаетwhat does it mean? method does not work? or does not work as you expect? - Alexey Shimansky