Noob need help. 😕😕

So I’ve been following some tutorials for my exam project. I am getting the hang of it.

What I was wondering is I am building a targetting system. In the tutorial he just uses white cubes. So when my script targets a white cube it turns red, untargetted it turns blue. But I would like to do the same with my Spider asset. Which is fully textured. but It doesnt get colored in anyway like a white cube does. Heres the script:

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

public class Targetting : MonoBehaviour {
	public List<Transform> targets;
	public 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);
	}

	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;
			}
			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();
		}
	}
}

I was also wondering, how can I put into the code to use some sort of crosshair type gui instead of just coloring the targets?

I am getting the scripts and tutorials from burgzergarcade.com
Great for beginners.

  1. Please don’t bump so quickly.
  2. Use the Code tags so it looks like this:
public void Example(){
    Debug.Log("This is much easier to read.");
}

Ok, I’ll remeber that. Thanks for the help. Ill test it out and post if anything else :slight_smile:

Okay, so do I understand you correctly?

You have a script that targets objects and you want the objects to be highlighted.
Besides that, you want to draw a crosshair on them.

For this you’ll need to take the position of the object that is being aimed at and get the screenposition through the WorldToScreenPoint() method.
Then you’d draw the crosshair there.

Ok thanks. But this script when TAB targeting an enemy it should turn that enemy red, and when cycling between targets, other targets turn blue. But this only works when my targets a white cubes. I have a textured animated spider which I would like to use instead of the cube. But the spider those not change color when Targeted.

Hmmm, since I didn’t need to change textures/materials through code yet, I cannot help you with this.
In meantime you could still work on the crosshair part though. Maybe someone else will jump in.

Ok, will try it out. Thanks alot :slight_smile:

What shader are you using on the spiders? You should be set the colour of the main texture or the texture itself.

selectedTarget.renderer.sharedMaterial.SetVector( "Variable Name", Color Here );
selectedTarget.renderer.sharedMaterial.SetTexture( "TextureName", Texture Here);

Figured it out. I just needed to create a tag for the inspector which had the texture. This inspector was a sub of the one containing animation and AI scripts.