Now I know this is an issue of dynamic typing not supported on iOS, but I just can not figure out the correct way to define a type to the script component?
What I want to do is similar to this:-
var prefabBullet : Transform;
var myEnemy : Transform;
var instanceBullet : GameObject;
function Update () {
if (Input.GetButtonDown("Fire1")){
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.velocity = rigidbody.velocity + (transform.forward *10);
instanceBullet.GetComponent(MyScript).enemy = myEnemy;
}
}
(ie. pass on the transform ‘myEnemy’ to the variable ‘enemy’ in the script that is attached to an instantiated prefab).
The code above fails at the last line: instanceBullet.GetComponent(MyScript).enemy = myEnemy;
I’m fairly sure that this is because ‘MyScript’ has not been typed? I’ve searched the forums and all the help docs for instantiaite, and although I can find a lot of help on typing for iOS, I can not find anything to help in the example above.
Although if I’m honest, I’m still unclear on why “as MyScript” is needed here though…? I get why static typing is used, it’s more efficient for the holder to know in advance what ‘type of thing’ it is going to hold and keep always so, but here, ‘MyScript’ isn’t really a ‘type’ is it? It’s just a name I gave to a script, right…?
Anyway, thanks again, I appreciate your speedy help…
GetComponent returns a Component. “enemy” isn’t a property of Component, it’s a property of MyScript, so you have to cast the return value of GetComponent to something you can use. Dynamic typing does that for you, but it has to figure it out on the fly, which is quite a bit slower than specifying it ahead of time in the script. “MyScript” is indeed a type, it’s not just a name.
I have this problem too
Unity said transform, rigidbody and collider are not members of the dynamic object
I can use the GetComponent(“XXX”) as XXX without problem but how about normal members of the game object?
Edit: Oh…I am extremely stupid…I found out that I can use “as GameObject” orz