I have an object that spawn’s enemies using a variable for which enemy to spawn. I can simply drag and drop a gameObject into the appropriate slot for my spawner and it will spawn that enemy. Now I’d like to assign a “movement type” (controls how that object moves) using a variable on the spawner. This way, I don’t have to create 10 copies of the same gameObject with the only difference being ‘how they move’.
var enemy : GameObject;
var moveScript : SCRIPT;
function Spawn(){
...
var obj = Instantiate(enemy,transform.position,transform.rotation);
obj.AddComponent(moveScript);
...
}
The problem is, no matter how I define my ‘moveScript’ variable, I get an error:
No appropriate version of ‘UnityEngine.GameObject.AddComponent’ for the argument list ‘(UnityEngine.Component)’ was found.
I’ve managed to make it work as a ‘string’ and manually type in the name of the desired script but I am convinced there is a way to do this by dragging and dropping the actual script file into that variable’s slot in the Inspector pane… Any ideas?
Have you tried defining moveScript as a Component or MonoBehaviour?
You don’t have to assign it in the Inspector. If your script derives from MonoBehaviour then the name is also the Type.
public class moveScript : MonoBehaviour {
///....
}
Then you can just do
obj.AddComponent(moveScript);
There’s nothing wrong with you defining the script, the error you get is only because Unity finds obj as just a simple variable not a GameObeject so try defining obj in the beginning as a GameObject;
var obj : GameObject;
function Spawn() {
obj = Instantiate(enemy,transform.position,transform.rotation);
obj.AddComponent(moveScript);
}
I’ve tried:
Component
MonoScript
MonoBehavior
Object
GameObject
And possibly others. They ALL give the same error:
No appropriate version of ‘UnityEngine.GameObject.AddComponent’ for the argument list ‘(UnityEngine.XXX)’ was found.
Where XXX = The types listed above.
@KelsoMRK
Yes, I do need to assign it in the Inspector, that’s the whole point. I want to drop a spawner at any given location and in the “moveScript” variable, I want to drop in the script that will control the movement of anything that spawner will spawn.
In the image:
The upper left shows my spawner in the scene and to the right of that, that spawner is selected in the Hierarchy pane. Following the arrow to the Inspector pane, you can see where I have the Spawn Wuzzle script and the variable moveScript highlighted. In the bottom center is my Project pane with some movement scripts highlighted. I want to drag those scripts (following the arrow) over to the ‘moveScript’ variable of the Spawn Wuzzle script in the Inspector.
Okay here’s a thought lol, why don’t you attach your movescript to the enemy GameObject then make a prefab then just instantiate that prefab???
Because then I need a different prefab of the same creature for each movement type. It’s better to have ONE creature and attach the associated movement type as needed. I plan to have a LOT more movement types using various Sin waves, targeting and not targeting the player, etc.
I do have this working if I change the “moveScript” variable to a string, but then I just need to be DAMN SURE I spell each one perfectly… It’s just so much nicer and faster to use drag and drop…
Well that definitely makes it complicated then 
Hence the forum post! LOL! Thanks for looking!
I’ll stick with the ‘string’ variable until I can get this figured out but for now I am moving on!
I wouldn’t do it that way. I would set up an enum that determines what script to add and then add that component in Awake().
Hmm, I have never used an ‘enum’ and honestly I have no idea what it is how it would be done…
public class ComponentLoader : MonoBehaviour {
public enum ComponentID
{
Comp1, Comp2, Comp3
}
public ComponentID ComponentToLoad;
void Awake()
{
if (ComponentToLoad == ComponentID.Comp1)
{
AddComponent<SomeComponent>();
}
else if (ComponentToLoad == ComponentID.Comp2)
{
// you get the point
}
}
}
The advantage is you get a dropdown in the Inspector so no typing errors.
Sweet. I don’t suppose you know how that would look in JavaScript?
enum ComponentID{Comp1, Comp2, Comp3}
var ComponentToLoad : ComponentID;
function Awake(){
if (ComponentToLoad == ComponentID.Comp1){
gameObject.AddComponent(SomeComponent);
}else if (ComponentToLoad == ComponentID.Comp2){
// you get the point
}
}
Sweeeeeeeet. I got it working perfectly. I still have to worry about spelling when I add each of the movement scripts to the enum list but with Intellisense on, life just got a whole lot easier! I’d still prefer drag and drop but this definitely will save me a lot of time and reduces chances of errors by a LOT.
Thanks everyone!!