BurgZerg Tutorial problem

I’ve been going through the Hack and Slash Tutorials, which have been very helpful in understanding the programming aspects of game design. He had switched to an earlier form of the editor, which may be where the problem lies. I went through the targeting script and it spat back three errors. I am having trouble tracking them down.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Targeting : MonoBehaviour {

public List<Transform> targets;
public 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, myTranform.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;
		}
		DeselectTarget();
		selectedTarget=targets[index];
		
	}
	SelectTarget();
}

private void SelectTarget(){

	selectedTarget.renderer.material.color=Color.red;
	
	PlayerAttack pa= (PlayerAttack)GetComponent("PlayerAttack");
	
	pa.target= selectedTarget.gameObject;
}

private void DeselectTarget() {

	selectedTarget.renderer.material.color=Color.blue;
	selectedTarget=null;
	
}
	
// Update is called once per frame
void Update () {
	if (Input.GetKeyDown(KeyCode.Tab)){	
	TargetEnemy();
	}
}

}
Assets/Scripts/Targeting.cs(35,26): error CS1503: Argument #2' cannot convert object’ expression to type UnityEngine.Vector3' Assets/Scripts/Targeting.cs(35,26): error CS1502: The best overloaded method match for UnityEngine.Vector3.Distance(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments
Assets/Scripts/Targeting.cs(35,48): error CS0103: The name `myTranform’ does not exist in the current context

Has something been deprecated?
Thanks!

So, I learned you could use delegate that way, thank you :slight_smile:

However, that’s what causing your problem. The function dynamically created by delegate does not belong to Targeting, thus can’t access MyTransform at all, and t1 is null. To make it even better, as the delegate declaration need the complete signature, you can’t give it arguments at the same time. So let’s see how we can do that differently.

To sort a list, you either give no arguments, a function (that was your delegate) or an object with a compare function. I guess the simplest here is the function. So create it like that :

private static int CompareDistance(Transform t1, Transform t2) {
    return Vector3.Distance(t1.position, myTranform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
}

Then, in the function SortTargetsbyDistance, targets.Sort( CompareDistance(/I dont know what t1 is for you/, myTransform ));