trying to create a turret that constantly searches for players error CS1525: Unexpected symbol `(', expecting `identifier', or `in'
hi gang trying o create a turret that constantly looks for players (since players are constantly being destroyed and created in my scene) find a random player and look at it. also need a way so it randomly selects a player from the list at random. could i use random.range for this?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AITurret : Photon.MonoBehaviour {
//----------------------------------------------------------------------------------------------------------
List<Players> player = new List<Players>();
public Transform PlayerSelected;
Quaternion realRotation = Quaternion.identity;
//----------------------------------------------------------------------------------------------------------
void Start ()
{
PlayerSelected = null;
}
//----------------------------------------------------------------------------------------------------------
public void AddplayertoList()
{
//makesurethis is done on masterclient
if (PhotonNetwork.isMasterClient) {
foreach(GameObject.FindGameObjectsWithTag("Player") //add a player for each gameobject with tag
Players.Add (player));
}
}
//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
public void TargetedPlayer()
{
if(SelectedTarget == null)
{
SelectedTarget = Players [0];
// need to be slected at random.
}
}
//----------------------------------------------------------------------------------------------------------
void LateUpdate ()
{
AddplayertoList (); //constently look at add player
TargetedPlayer();
float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
//if(dist <150)
//{
Vector3 relativePos = SelectedTarget.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 5c4r3cr0w · May 07, 2016 at 04:45 AM
You need to take a single GameObject as reference in foreach loop. Like:
foreach(GameObject g in GameObject[] /* Array goes here*/) {
//Do your Stuff here
}
for ref check this: https://msdn.microsoft.com/en-in/library/ttw7t8t6.aspx
Answer by aditya · May 07, 2016 at 05:10 AM
Please do not read this answer but do read the conversation below
public void AddplayertoList(){
if(PhotonNetwork.isMasterClient){
foreach(GameObject player in GameObject.FindObjectsWithTag("Player")){
GameObject tempObject = player; // Never ever remove this line or you never gonna get your desired results
Players.Add(tempObject);
}
}
}
Never ever remove this line or you never gonna get your desired results
Uhm this line is pointless in this case. We don't have a closure here that could cause problems. You can savely remove it ^^.
some time ago me also stuck in somewhat-same problem where i was assigning gameobjects inside foreach loop and it was always returning the last gameobject from array, that is why i wrote this line in my answer ... This post here helped me to overcome that problem
Again, the post you've linked there uses closures which is the actual problem. In our case here there is no closure / lambda / anonymous method. It seems you spread wrong information you don't fully understand yourself.
Your answer
Follow this Question
Related Questions
problem with loading list of strings on buttons 2 Answers
Deleting an object from a list of objects from another script. 1 Answer
null referance when adding to a list please help 1 Answer
How would I implement a random spawn timer into a list based spawn system? 2 Answers
List, for loop and removal of items 2 Answers