- Home /
An issue with GetComponent
I'm in the process of neatening up my scripts to package it all together into one prefab, which basically consists of a main controller, a rocket launch system and some turrets. So far, I'm working on getting the main controller to find a target and then assign it to the other components. Turrets work, the missile launcher will trigger based on the correct target.
However, the missiles are a different matter. They're prefabs instantiated from a series of spawn points, and use their own targeting system. I'm attempting instead to pass the target to them from the controller.
In the script for my launcher, I have this:
var missileProjectile : Rigidbody = Instantiate(
projectile, slot.position, slot.rotation ); //create clone of projectile at the right position/orientation
missileProjectile.velocity =
transform.TransformDirection( Vector3.forward * speed ); //give it some speed
and then I attempt to pass it the target:
missileProjectile.GetComponent(missileHomingScript).missiletarget = LookAtTarget ;
Where missileHomingScript is the script attached to the prefab, and LookAtTarget is the name of the target.
Unity throws me this error: Assets/Standard Assets/Scripts/Component Scripts/Weapon Component/multi_missile_launcher_script.js(88,40): BCE0005: Unknown identifier: 'missileHomingScript'.
I've scoured every resource I can find, and I'm pretty certain that the syntax is correct. So why am I getting this error?
Answer by Bunny83 · Feb 03, 2012 at 07:06 PM
I guess "missileHomingScript" is outside of the "Standard Assets" folder. Scripts in "Standard Assets" are compiled before all "normal" scripts like you can read in Eric's link. You should either move your "multi_missile_launcher_script" out of "Standard Assets" or put your "missileHomingScript" also in Standard Assets.
I would strongly recommend to move them out of this folder. This folder should only be used for complete script packages that don't belong on other things. That way they are available to all languages.
That did it! Thanks a lot! I knew it would be something simple. Looks like I'd better get busy reorganising my assets.
Answer by Eric5h5 · Feb 03, 2012 at 06:23 PM
There's nothing called "missileHomingScript" that is available to the script when being compiled. Either that's a typo (the convention is to use capital letters for classes, so I would have called it MissileHomingScript instead), or else it's a C# script that's not visible to the JS script (see here).
Thanks for the response! missileHo$$anonymous$$gScript is the script attached to the missile, the one that I'm trying to pass the target variable to. It's definitely not a C# script! I've got a similar system to work with other prefabs, so I think my best course of action at this point is to do some rewriting and see how it comes out.