Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed May 07, 2016 at 05:11 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
-3
Question by zak666 · May 07, 2016 at 05:59 AM · listscript errorrandom.range

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();
 
 
     }
 
 }
 
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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)];
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges