- Home /
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();
}
}
}
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
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
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();
}
}
}
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.
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
Your answer
Follow this Question
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