• 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 /
avatar image
-1
Question by zak666 · May 07, 2016 at 03:42 AM · listscripterror

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();
 
 
     }
 
 }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

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
avatar image
1
Wiki

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);
         }
     }
 }
Comment
Add comment · Show 8 · 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
avatar image Bunny83 · May 07, 2016 at 05:50 AM 0
Share

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 ^^.

avatar image aditya Bunny83 · May 07, 2016 at 06:33 AM 0
Share

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

avatar image Bunny83 aditya · May 07, 2016 at 06:51 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

45 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 avatar image

Related Questions

Destroy GameObject in list then instantiate GameObject not working 1 Answer

How to create a Matrix / Array / List / Grid of gameObjects? 1 Answer

Moving from Javascript to C# Error - does not contain a constructor that takes `x' arguments 1 Answer

Adding a unique element from one list to another list 2 Answers

Help Regarding Making A Server List by Instantiating prefabs 0 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