Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DanCarreras · Apr 18, 2011 at 11:50 PM · gameobjectrigidbodyios

Problems compiling script for iPhone that don't appear when running in unity.

Hi all,

Hoping someone can help me here, keep getting errors when I build my code to iOS, it's this sample of code which screws up on me:

var bulletPrefab:Transform;

 function Update ()

{ if(Input.GetButtonDown("Fire1")) { if(Collisions.BULLET_AMMO > 0) {

     //create the bullet Prefab
     var bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);

     //add force to the bullet
     bullet.rigidbody.AddForce(transform.forward * 2000);

     Collisions.BULLET_AMMO --;
     GameObject.Find("G_Ammo").guiText.text = ""+Collisions.BULLET_AMMO;
     }

 }

}

Now I've tried writing:

var bullet : GameObject = Instantiate(bulletPrefab, transform.position, Quaternion.identity);

But when I do it stops me applying force to the rigidbody, stating that it cannot cast from source type to destination type.

This all works fine when I'm running the game within unity, but the second I go to compile to iOS I get errors pointing to these!

If anyone could help I would be eternally grateful,

many thanks

Dan

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Peter G · Apr 19, 2011 at 02:28 AM

var bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);

//add force to the bullet bullet.rigidbody.AddForce(transform.forward * 2000);

This is called dynamic typing. Since you have not declared a type for bullet, Unity will infer that it is an Object because that is what Instantiate() returns. Declaring a type like you did will partially solve this problem. BUT that leaves the InvalidCastException which will be explained below.

Then, by calling .rigidbody, Unity has to figure out if this object supports that getter, and if it does then use it. This is also a dynamic feature. Both of which are supported on the desktop since it is JIT compiled but not on the iOS which is AOT compiled.

The Fix

var bullet : GameObject = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as GameObject; //You need a cast to GameObject from Object since there is a chance of losing data.

//add force to the bullet bullet.rigidbody.AddForce(transform.forward * 2000); //Now we know that bullet is a GameObject and supports the .rigidbody getter.

You have to declare a type for bullet like you did above. Now Unity knows that it is a GameObject. But, you will get an InvalidCastException because there is the risk of losing data. In this case, you have an object that stores information about everything associated with the object you just created. By casting to a GameObject, this reference is losing all information that isn't a property or method of GameObject (Although in this case you can still access the information through GameObject's properties such as rigidbody). So you have to tell the compiler that you know you could lose data and that it should do it anyway.

2 more points

  • On iOS, memory is an issue, you don't want to instantiate new objects during gameplay if you don't have to. If you can, create a system that reuses old objects and switches them on and off.

  • You can cast directly to rigidbody then not have to use the getter to call AddForce()

EX:

bullet : Rigidbody = Instantiate() as Rigidbody;
bullet.AddForce();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by kgoswami · Sep 19, 2011 at 10:49 PM

do this

 var bullet : Transform= Instantiate(bulletPrefab, transform.position, Quaternion.identity) as Transform;
 
 //add force to the bullet
 bullet.gameObject.rigidbody.AddForce(transform.forward * 2000);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Sep 19, 2011 at 11:39 PM 0
Share

you just posted a duplicate answer to a question that was answered months ago. Seriously don't do that.

avatar image kgoswami · Sep 19, 2011 at 11:46 PM 0
Share

@syclamoth. I tried all the answers above and it did not work for me. I was having the same problem. I went to our university professor and he fixed my problem with the solution that I posted. i did not see an approved answer on this page, so I decided to help the user about it. Sorry for causing the inconvenience. $$anonymous$$y answer works perfectly fine, as i have the EXACT SA$$anonymous$$E problem and it fixed it!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to tilt the gameobject based of Input.Acceleration? 0 Answers

"Some objects were not cleaned up when closing the scene" 1 Answer

Ignore Collider 0 Answers

Adding local created rigedbody to gameobject 1 Answer

My rigidbody is floating away, its a child of a parent 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges