- Home /
SelectTarget Does not exist Current Context
Hi there first of all i want to say i have been following BergZerg arcade tutorial on rpg game. But on tutorial 08-10 i ran into a problem as stated as the title. Here is the image on what the problem is
and here is the code
using UnityEngine;
using System.Collections; using System.Collections.Generic;
public class targeting : MonoBehaviour { public List targets; private Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2)
{ return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); });
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
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;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectedTarget();
}
private void SelectTarget()
{
SelectedTarget.renderer.Marterial.color = Color.red;
playerattack pa = (playerattack)GetComponent("playerattack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget()
{
selectedTarget.renderer.Marterial.color = Color.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
What is 'SelectedTarget'? is it a method, or an object, or a delegate, or what? I don't see where you declare it, and you can't seem to be able to decide what kind of thing it is.
Answer by Manny Calavera · Dec 12, 2011 at 04:53 AM
There are a couple of typos on your script:
You declare a private variable called "selectedTarget" but in the SelectTarget() method you refer to it as SelectedTarget -> Note the capital S.
The other problem is that you declared "SelectTarget()" method but then you call "Select*ed*Target()" -> Note the ED in the name
thanks for that big pointer but how do i solve the material problem? and yes i already found out it was a typo.. but then it still say the same thing >_<
Your answer
![](https://koobas.hobune.stream/wayback/20220613062008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
WHY SAYS ME THAT IS AN ERROR? 1 Answer
what am I doing wrong 1 Answer
Microsoft C# Card Game Starter Kit 0 Answers