Targeting Script

Okay so what im trying to do is make it so that when the raycast hits an object its able to determine what it is. So like Mage, Warrior, Archer, etc. And then code it to say if Mage(for example) was selected activate this script. I already know the code of how to activate and deactivate scripts, so all i really need help with is on how to make the script recognize what was selected. And then how to say if-this-object-was-selected then… Thanks in advance!!!

using UnityEngine;
using System.Collections;

public class HeroTargeting : MonoBehaviour {
	
	public Transform selectedTarget;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		 if (Input.GetMouseButtonUp(1)){ // when button clicked...
      RaycastHit hit; // cast a ray from mouse pointer:
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      // if enemy hit...
      if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Hero")){
        DeselectTarget(); // deselect previous target (if any)...
        selectedTarget = hit.transform; // set the new one...
        SelectTarget(); // and select it
      
		
		
			}
    }
  }

  private void SelectTarget(){
   
			
	}
	

  private void DeselectTarget(){
    if (selectedTarget){ // if any guy selected, deselect it
     
      selectedTarget = null;
    
		}
	
		}
	
	}

You could have a basic class which contains the player’s stats.
like, hp,mana,class(warrior,etc),etc. class this BaseChar script.

Create your multiple objects which could be multiple players.
Something like…

player1 = BaseChar()
player2 = BaseChar()

then, you could use a Ray to check collision. Create a ray when mouse button is pressed. Have this ray ‘point’ out to into the game world which will make an invidsble line and have another script check for collision. Then when a collision has occurred.

OnCollisionEnter( object : Collider ){
   object.GetComponent(BaseChar)   // <-- this gives you the character stats

   // so say you have a function to display all the properties of the class like..
   object.GetComponent(BaseChar).printAll() // which could print every variable you want
}

You will need to use Raycasting to check when the collision hits some object Unity - Scripting API: RaycastHit.distance

I am not 100% sure on the exact code needed. You will need to look up Ray and RaycastHit.

@cdrandin’s answer does answer your questions, but he is also showing you a good way of structuring the data and routines assiciated with a specific character. Here is a simpler example (done in C# sorry): Make a script that is empty for single global variable called type.

public class CharacterType : MonoBehaviour
{
    public int type = 0;
}

In Javascript I think you can do this in a single line like “var type : int = 0;” Attach this script to all your character objects. Set the value of type in the inspector to different values for different character types: 0 - Mage, 1 - Warrior, 2 - Archer, etc;

Now to get that data from a collision you do something like this (C# again)

RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
	CharacterType ct = hit.collider.gameObject.GetComponent<CharacterType>();
    if (ct != null)
	    Debug.Log ("Character Type = "+ct.type);
}

Using the RaycastHit and GetComponent(), you can get access to any script attached to the game object, and the information about your character can be stored with the object.

HI,You can do it two ways one is by creating an enum other by using tags.if you want to use tag
Here is the script
By the way what is "out hit"i didn’t see you declare it any where and dont use && hit.transform.CompareTag(“Hero”)) after Physics.Raycast because if you do so it only casts a ray only when its an object tagged(“Hero”).Since there is no raycasting it cant check wether it has hit an object with tag(Hero) or not
The ray shooting script(Alter these in your script)

Transform selectedTarget;


void Update(){
    if (Input.GetMouseButtonUp(1)){ // when button clicked...
          RaycastHit hit; // cast a ray from mouse pointer:
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          // if enemy hit...
              if (Physics.Raycast(ray,hit,10000)){
    if(hit.collider.tag == "Hero"){
    DeselectTarget(); // deselect previous target (if any)...
                 selectedTarget = hit.transform; // set the new one...
    
       SelectTarget(selectedTarget); // and select it
    }
        }
      }
    }
    function SelectTarget(Transform obj){
    obj.GetComponent<Hero_Script>().selected = true;/////you can replace Hero_Script with your script name
    }
    Transform[] Army;///Add your hero and all other AI to this array
    
    DeselectTarget(){
    foreach(int obj =0;obj< Army.Length;obj++){
    if(Army[obj]!= selectedTarget){
    Army[obj].GetComponent<Your AI Script name>().selected = false;
    }
    }
    }

Now just add this to your Hero or Mage script

bool selected;

//your other code
void Update(){
if(bool){
///your code goes here

}else{
///your code to make the Ai go to idle animation 
}
}