javascript - Error "InvalidCastException: Cannot cast from source type to destination type." While using Instantiate in Unity -


okay, i'm making 2d game in unity, , when run code, error:

invalidcastexception: cannot cast source type destination type. toastspawn.spawntoast () (at assets/scripts/toastspawn.js:11) 

the code in file toastspawn.js follows:

#pragma strict  var toast : rigidbody; var toastspawner : gameobject;  function start() {     invokerepeating("spawntoast", 3, 1); }  function spawntoast() {     var toastclone = instantiate(toast, gameobject.find("toastspawner").transform.position, quaternion.identity);     toastclone.addforce(vector2 (0,1) * 1000); } 

i started using unity yesterday, why getting error. :)

well, it'd better if take start c# :p although not javascript guy still can see glitches.

  • you instantiating rigidbody instead of gameobject. take gameobject instead of rigidbody, getcomponent of rigidbody object in order apply force.
  • you have toastspawner gameobject why or using gameobject.find? same gameobject.

something like,

var toast : gameobject; var toastspawner : gameobject; ... ...  var toastclone = instantiate(toast, toastspawner.transform.position, quaternion.identity); var rb : rigidbody = toastclone.getcomponent("rigidbody"); rb.addforce(...); 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -