- Home /
External script access
Hi guys,
How i can access other scripts with a string var in c#;
I have a lot of enemies and each enemie have a different script, but all of them have the same function to have damage.
if(hit.gameObject.name=="zombie"){
hit.getComponent<AIscriptZombie>().hit(var1, var2, var3,etc);
}
if(hit.gameObject.name=="vampire"){
hit.getComponent<AIscriptVampire>().hit(var1, var2, var3,etc);
}
if(hit.gameObject.name=="archer"){
hit.getComponent<AIscriptArcher>().hit(var1, var2, var3,etc);
}
How i can make this simply like this:
string scriptname = "AIscript"+ hit.gameObject.name;
hit.getComponent().hit(var1, var2, var3,etc);
I learned unity alone and there are little tricks that I don't know xD.
Please one help :D
Thanks...
Answer by Graham-Dunnett · May 25, 2013 at 10:02 PM
Use the version of GetComponent
that takes a string argument, see second example at http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html.
This doesn´t work.
The type or namespace name `ScriptName' could not be found. Are you missing a using directive or an assembly reference
my code:
ScriptName other;
other = gameObject.GetComponent("AIscript"+hit.gameObject.name) as ScriptName;
nLastHits=(other.hit(tempanim, tempEmp, tempVida, tempDef, tempTemp, tempGrav, tempType, tempDes, id, gameObject.transform));
$$anonymous$$y all enemies had a script called AIscript+enemie name, and each time I hit them in combo i need to call a function hit that each script have.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102924im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character Not Rotating 0 Answers
C# Shooting a cannonball (ballistics) 1 Answer
need teleport script fixed slightly 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers