The question is answered, right answer was accepted
random.range to get Random player in list? he best overloaded method match for `System.Collections.Generic.List.this[int]' has some invalid arguments
hi I've managed to get a list of all game object players having errors trying to find Random.rang in a list. is it possible to get a random player in a list with random.range? for a turret to lookazt.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AITurret : Photon.MonoBehaviour {
//----------------------------------------------------------------------------------------------------------
public List<Transform> Players = new List<Transform>();
public Transform PlayerSelected;
Quaternion realRotation = Quaternion.identity;
private float distance;
//----------------------------------------------------------------------------------------------------------
void Start ()
{
PlayerSelected = null;
}
//----------------------------------------------------------------------------------------------------------
public void AddplayertoList()
{
GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject _Player in ItemsInList)
{
AddTarget(_Player.transform);
}
}
public void AddTarget(Transform player)
{
Players.Add(player);
}
//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
public void TargetedPlayer()
{
if(PlayerSelected == null)
{
PlayerSelected = Players[Random.Range(0, Mathf.Round(Players.Count - 1))];
if (distance > 400) {
Debug.Log ("Player Is Out Of Range");
TargetedPlayer (); // if the chosen player is out of range select another player.
}
}
//randomly select a target.
}
//----------------------------------------------------------------------------------------------------------
void LateUpdate ()
{
AddplayertoList (); //constently look at add player
TargetedPlayer();
distance = Vector3.Distance(PlayerSelected.transform.position,transform.position);
//if(dist <150)
//{
Vector3 relativePos = PlayerSelected.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
this.transform.rotation = rotation;
//}
if (photonView.isMine) {
} else {
Quaternion realRotation = Quaternion.identity;
}
}
//----------------------------------------------------------------------------------------------------------
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (PhotonNetwork.isMasterClient) {
stream.SendNext(transform.rotation);
}
else{
this.realRotation = (Quaternion) stream.ReceiveNext();
}
}
}
Answer by Bunny83 · May 07, 2016 at 06:20 AM
Please stop posting almost the same wall of text as new question after every small progress. Also those are not technical questions about Unity but debugging questions. They don't belong here. There's an extra help room for questions like that.
Next thing is if the compiler throws an error at you, it tells you exactly where in the code the error is. You have not included the line number of the error, so we have to search your whole code to find the problematic line. You should put a little more efford in your questions if you want them to get answered.
On topic: Your problem is here:
PlayerSelected = Players[Random.Range(0, Mathf.Round(Players.Count - 1))];
Why do you use Mathf.Round here? "Players.Count" is an integer. It can't have a fractional part. Because you use Mathf.Round you automatically use Random.Range with float values instead of int values. However a float can't be used as index for an array or list. Next thing is the int-version of Random.Range has the max value exclusive, so you don't need a "-1". Just do:
PlayerSelected = Players[Random.Range(0, Players.Count)];
Follow this Question
Related Questions
Random Teleport Character issue...Help Please. 1 Answer
How I randomise a list of signals in Unity and remove it from the list once the clip is finished? 0 Answers
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers
Assign role randomly from array for my online game 2 Answers
How do I get random answers? 2 Answers