Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by UANP_Joe · Jul 13, 2013 at 12:29 PM ·

C# Targetting Script

Current code with all corrections

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Targetting : MonoBehaviour
 {
     public List<Transform> targets;
     public Transform selectedTarget;
     
     //Stuff that keeps messing up Part 1
     private Transform myTransform;
     
     //Use this for initialization
     void Start () 
     {
         targets = new List<Transform>();
         selectedTarget = null;
         //Stuff that keeps messing up Part 2
         myTransform = transform;
         
         AddAllEnemies();
     }
     public void AddAllEnemies()
     {
         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
         
         foreach(GameObject enemy in go)
             AddTarget(enemy.transform);
     }
     public void AddTarget(Transform enemy)
     {
         targets.Add(enemy);
     }
     //Stuff that keeps messing up Part 3
     private void SortTargetsByDistance()
     {
         targets.Sort(delegate(Transform t1, Transform t2)
         { return (Vector3.Distance(t1.position, myTransform.position).CompareTo)
         (Vector3.Distance(t2.position, myTransform.position));
         });
     }
     private void TargetEnemy()
     {
         if(selectedTarget == null)
         {
             //Stuff that keeps messing me up Part 4
             SortTargetsByDistance();
             selectedTarget = targets[0];
         }
     }
     //Update is called once per frame
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.Tab))
         {
             TargetEnemy();
         }
     }
 }
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

Answer by Benproductions1 · Jul 13, 2013 at 12:33 PM

Hello,

Just before line 42, on line 39 you are missing a } to close off your delegate.

If you need a link on how delegates work, check one of these:
http://forum.unity3d.com/threads/150321-C-delegates-I-love-you
http://answers.unity3d.com/questions/205295/question-about-delegates.html

You are also missing another } on line 50. (found by @supercouge)

Always remember to close off your methods XD

Hope this helps,
Benproductions1

Comment
Add comment · Show 14 · 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 supercouge · Jul 13, 2013 at 12:36 PM 1
Share

Also, you have a similar problem at line 51.

avatar image Benproductions1 · Jul 13, 2013 at 12:39 PM 0
Share

Updated the answer, thanks for that ;)

avatar image UANP_Joe · Jul 13, 2013 at 12:53 PM 0
Share

I changed the script to fix what you said which is now (lines 35-59) but I get new errors:

Assets/Scripts/Targetting.cs(38,17): error CS1026: Unexpected symbol }', expecting )'

Assets/Scripts/Targetting.cs(41,15): error CS1026: Unexpected symbol private', expecting )'

Assets/Scripts/Targetting.cs(52,14): error CS0116: A namespace can only contain types and namespace declarations

Assets/Scripts/Targetting.cs(59,1): error CS8025: Parsing error

avatar image UANP_Joe · Jul 13, 2013 at 12:53 PM 0
Share

the new codes lines 35-59:

 private void SortTargetsByDistance()
 {
     targets.Sort(delegate(Transform t1, Transform t2) { return (Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.positionmyTransform.position))
     });
 }
     
 private void TargetEnemy()
 {
         
     if(selectedTarget == null)
     {
         SortTargetsByDistance();
         selectedTarget = targets[0];
     }
 
 }
 // Update is called once per frame
 void Update() {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
     {
         TargetEnemy();
                             
     }
 }

}

avatar image Benproductions1 · Jul 13, 2013 at 12:57 PM 0
Share

Check your () and {} pairs. $$anonymous$$ake sure every ( or { has a matching ) or }.
A easy way of checking this is to use indentation matching your () and {} pairs for longer lines.

PS: you are missing a ) on line 3, but you should be able to find this out yourself.

Show more comments
avatar image
0

Answer by species5618 · Jul 24, 2013 at 01:01 PM

For those who still like to know, the script seems to be incomplete. Its seems you cannot scroll 2 the found enemy neither can you see ingame who is selected. I have alter this script and added the extra code. The tutorial on this can be found on youtube (not made by me)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
  
 public class Targetting : MonoBehaviour
 {
     public List<Transform> targets;
     
     // do not manual select the targets, this will be filled by the scripted with object that have the defines tag
     public Transform selectedTarget;
     public string targetTag = "Enemy";
  
     //Stuff that keeps messing up Part 1
     private Transform myTransform;
  
     //Use this for initialization
     void Start () 
     {
        targets = new List<Transform>();
        selectedTarget = null;
        //Stuff that keeps messing up Part 2
        myTransform = transform;
  
        AddAllEnemies();
     }
     
     public void AddAllEnemies()
     {
            GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag);
  
            foreach(GameObject enemy in go)
         {
             AddTarget(enemy.transform);
         }
     }
     
     public void AddTarget(Transform enemy)
     {
        targets.Add(enemy);
     }
     
     //Stuff that keeps messing up Part 3
     private void SortTargetsByDistance()
     {
        targets.Sort(delegate(Transform t1, Transform t2)
        { 
             return (Vector3.Distance(t1.position, myTransform.position).CompareTo)
                (Vector3.Distance(t2.position, myTransform.position));
        });
     }
     
     private void TargetEnemy()
     {
        if(selectedTarget == null)
        {
              //Stuff that keeps messing me up Part 4
              SortTargetsByDistance();
              selectedTarget = targets[0];
        }
         //// EDIT: Add this code; now we can jump 2 all the found enemy's by pressing TAB
         else
         {
             int index = targets.IndexOf(selectedTarget);    
             
             // because it start at 0 so that acual already 1
             if (index < targets.Count -1)
             {
                 index++;
             }
             // if we are at the LAST enemy in ar found list, jump back to the first enemy in the list
             else
             {
                 index = 0;    
             }
             
             //// EDIT: Add this; before we show/select the target, deselect your old target.
             ShowDeSelectedTarget();
             
             selectedTarget = targets[index];
         }
         //// EDIT: Add this; shows your currently selected target on the screen, using a color change.
         ShowSelectedTarget();
     }
     
     
     //// EDIT: Add this
     private void ShowSelectedTarget()
     {
         selectedTarget.renderer.material.color = Color.red;
 
         // this is used if your going to used the a "player-attack-script"
         // PlayerAttack attack = (PlayerAttack)GetComponents("PlayerAttack");
         // attack.target = selectedTarget.gameObject:
     }
     
     
     //// EDIT: Add this
     private void ShowDeSelectedTarget()
     {
         selectedTarget.renderer.material.color = Color.blue;
         selectedTarget = null;
     }
     
     //Update is called once per frame
     void Update () 
     {
            if(Input.GetKeyDown(KeyCode.Tab))
            {
              TargetEnemy();
            }
     }
 }
 


Note you do need to have a mesh render property on all the selected objects, else the script cant alter the color.

Note: the object you want to select needs to have a target tag, like enemy as defined here in the script.

Note: script can be places on you FPScontroller or OVRPlayerController (for the Oculus VR users :) )

good luck, this script is tested and it worked on unity4

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

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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Multiple Cars not working 1 Answer

Attach object as child using code 1 Answer

Changing material Color using RBG? 3 Answers

Unity and Leap Motion: Help with Jerkiness of input! 1 Answer

JavaScript help 1 Answer


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