Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scripting in C# error help?(using burgzerg arcade hack n slash tutorials)

Discussion in 'Scripting' started by SameDev, Apr 21, 2013.

  1. SameDev

    SameDev

    Joined:
    Apr 14, 2013
    Posts:
    16
    I cannot seem to find the error in the targetting script I am recreating from the burgzerg arcade tutorials for the hack and slash. If anyone could point out my error it would be very much appreciated. By the way, this is the tutorial I was on when I go stuck: http://www.youtube.com/watch?NR=1&v=P-Jpxdl7AR8&feature=endscreen

    2 of the lines have red underlines however I cannot seem to figure out the error. Unity is telling me the error is an unexpected symbol } and a parsing error.

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    public class PlayerTargetting : MonoBehaviour {

    public List <Transform> targets;
    private Transform SelectedTarget;
    private Transform myTransform;

    // Use this for initialization
    void Start () {

    targets = new List<Transform>();
    AddAllEnemies();
    SelectedTarget = null;
    myTransform = transform;
    }
    public void AddAllEnemies()
    {
    GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    foreach(GameObject Enemy in go)
    AddTarget(Enemy.transform);
    }
    public void AddTarget(Transform Enemy)
    {
    targets.Add(Enemy);
    }
    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)
    {
    SortTargetsByDistance();
    SelectedTarget = targets[0];
    }
    else
    {
    int index = targets.IndexOf(SelectedTarget);

    if(index < targets.Count - 1)
    {
    index++;
    }
    else
    {
    index = 0;
    }
    SelectedTarget = targets[indexer];
    }
    }

    private void SelectTarget();

    SelectedTarget.renderer.material.color Color.red
    } (This line has a red underline)

    // Update is called once per frame
    void Update () { (This line has a red underline)
    if(Input.GetKeyDown(KeyCode.Tab))
    {
    TargetEnemy();
    }
    }
    }
     
  2. SameDev

    SameDev

    Joined:
    Apr 14, 2013
    Posts:
    16
    PS: I apologize for the horrible format, its what happened when I copied and pasted the code
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    unexpected symbol } means you put a closed a brace in the wrong place
    and the easiet wasy to fix them is better formatting in your IDE.

    Make sure theres an equal number of {'s and }'s
    FYI Theres a code tags button when you make a post.

    Code (csharp):
    1. /////////you probably need a {  for this function
    2. private void SelectTarget();
    3.  
    4. SelectedTarget.renderer.material.color Color.red
    5. } (This line has a red underline)
    6.  
    7.  
     
  4. SameDev

    SameDev

    Joined:
    Apr 14, 2013
    Posts:
    16
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class PlayerTargetting : MonoBehaviour {
    5.    
    6.     public List <Transform> targets;
    7.     private Transform SelectedTarget;
    8.     private Transform myTransform;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.         targets = new List<Transform>();
    14.         AddAllEnemies();
    15.         SelectedTarget = null;
    16.         myTransform = transform;
    17.     }
    18.     public void AddAllEnemies()
    19.     {
    20.         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    21.             foreach(GameObject Enemy in go)
    22.                 AddTarget(Enemy.transform);
    23.     }  
    24.     public void AddTarget(Transform Enemy)
    25.     {
    26.         targets.Add(Enemy);
    27.     }
    28.     private void SortTargetsByDistance()
    29.     {
    30.         targets.Sort(delegate(Transform t1, Transform t2){
    31.             return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
    32.             });
    33.     }
    34.     private void TargetEnemy()
    35.     {
    36.         if(SelectedTarget == null)
    37.             {
    38.                 SortTargetsByDistance();       
    39.                 SelectedTarget = targets[0];
    40.             }
    41.             else   
    42.             {
    43.             int index = targets.IndexOf(SelectedTarget);
    44.        
    45.             if(index < targets.Count - 1)
    46.             {
    47.                 index++;
    48.             }
    49.             else
    50.             {
    51.             index = 0; 
    52.             }
    53.         SelectedTarget = targets[indexer];
    54.         }
    55.     }
    56.     private void SelectTarget();
    57.    
    58.         SelectedTarget.renderer.material.color  Color.red
    59. {}                                                                                                     //this line has a red line now. Deleting them makes
    60.    
    61.     // Update is called once per frame
    62.     void Update () {                                                                         //this line have an error
    63.         if(Input.GetKeyDown(KeyCode.Tab))
    64.         {
    65.             TargetEnemy();
    66.         }
    67.     }
    68. }
    I tried that and I got this
     
  5. SameDev

    SameDev

    Joined:
    Apr 14, 2013
    Posts:
    16
    You may need to scroll to the right for errors
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    still missed it

    {} does nothing

    Code (csharp):
    1.  private void SelectTarget() //no line ender ; here
    2.  
    3.    { //open bracket for function
    4.  
    5.         SelectedTarget.renderer.material.color  Color.red
    6.  
    7. }   //close bracket for function                        
     
  7. SameDev

    SameDev

    Joined:
    Apr 14, 2013
    Posts:
    16
    That just puts a red underline under this line :/

    SelectedTarget.renderer.material.color Color.red
     
  8. scrawk

    scrawk

    Joined:
    Nov 22, 2012
    Posts:
    804
    Replace

    Code (csharp):
    1.     private void SelectTarget();
    2.  
    3.         SelectedTarget.renderer.material.color  Color.red
    4.  
    5. {}
    With

    Code (csharp):
    1.  
    2. private void SelectTarget()
    3. {
    4.         SelectedTarget.renderer.material.color  = Color.red;
    5. }