- Home /
Getting a Script without setting a variable to the script name
Hi,
I am making a script that can be used in multiple gameobjects, but requires a different script for different gameobject. For Example, a player would have a movement script input and an enemy would have AI script. This is what i am trying to achieve:
Script Movement;
if (transform.name == "Player")
Movement = GetComponent<MovementScript>();
else if (transform.name == "Enemy")
Movement = GetComponent<AIScript>();
How can this be done?
Answer by YoungDeveloper · Jul 13, 2014 at 12:20 PM
Don't think so, you have to think more object oriented for these kind of things. I wrote an example for beginners how to manage item structures, http://armedunity.com/topic/6721-basic-idea-of-creating-any-item-list-in-game/
Same can be used for enemies for finding out the type. When you know the type you can use good old switch case.
This will work for simple things.
private void HitObjectHandler(GameObject go){ //receive object from ray hit (example)
switch(go.tag){
case "Npc":
NpcType npcScript = go.GetComponent<NpcType>();
FindType(npcScript.index);
break;
}
}
public void FindType(short arg){
switch(arg){
//Player
case 0:
Debug.Log("its a player");
break;
//Zombie
case 1:
Debug.Log("its a dead meat");
break;
//Ninja kitten
case 2:
Debug.Log("its a ninja");
break;
}
}
Your answer
Follow this Question
Related Questions
Why are these object passing through each other? 1 Answer
Retrieving a variable from ALL scripts on a Game Object 1 Answer
Moving GameObjects with mouse and check contains 0 Answers
Instantiated Prefab using its transform and object on other scripts 1 Answer
Simple FPS Movement Not Working 0 Answers