• 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 conflictbliz · Nov 15, 2011 at 06:55 PM · destroyboxcollider

spawning a box collider

var projectile : Rigidbody;
var speed = 20;
function Update()
{
 if( Input.GetButtonDown( "Fire1" ) )
  {
  var instantiatedProjectile : Rigidbody = Instantiate( 
   projectile, transform.position, transform.rotation );
  instantiatedProjectile.velocity =
   transform.TransformDirection( Vector3( 0, 0, speed ) );
  Physics.IgnoreCollision( instantiatedProjectile. collider,
   transform.root.collider );
  }
}

i want it so that i can spawn a box without a rigidbody but i am not sure how to take the rigidbody away inside the script.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Nov 15, 2011 at 09:25 PM

If all you want is to spawn a cube in front of the camera, add this script to the camera:

function Update(){
  if( Input.GetButtonDown( "Fire1" ) ){
    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    // set the position 1 meter ahead of the camera
    cube.transform.position = transform.position + transform.forward;
    cube.transform.rotation = transform.rotation;
  }
}
If you want to instantiate an arbitrary object, use this:

var prefab: Transform; // drag the object here

function Update(){ if( Input.GetButtonDown( "Fire1" ) ){ Instantiate(prefab,transform.position+transform.forward,transform.rotation); } }

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 Mox.du · Nov 15, 2011 at 07:14 PM

Here you go.

 // attach cube with rigidbody to cube_pfb var.
 var cube_pfb:GameObject;
 
 function Start () {
     var newCube:GameObject =  Instantiate(cube_pfb, Vector3(20,20,20), transform.rotation);
     Destroy (newCube.GetComponent (Rigidbody));
 }

Regards.

Comment
Add comment · Show 3 · 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 conflictbliz · Nov 15, 2011 at 07:45 PM 0
Share

it did not work, i tried adding it too the prefab to see if anything happens and its clones the cube and spams my game until its frozen i wanted it just to spawn a cube infront of the camera that had only a box collider.

avatar image Mox.du · Nov 15, 2011 at 09:57 PM 0
Share

$$anonymous$$ate, initially you said that you want to remove rigidbody from object, and now you are saying that you have only collider on your cube? $$anonymous$$y script is doing what you initially asked for, removing rigidbody from the object via script. To test your object must have rigirbody attached to it!

avatar image conflictbliz · Nov 15, 2011 at 11:07 PM 0
Share

ok yes i have another script though and it spawns the cube with a rigidbody the problem is when i do spawn it, it makes clones and then freezes the game

avatar image
0

Answer by Mox.du · Nov 15, 2011 at 09:19 PM

It is working 100%. Try this in empty scene for test purposes. Don't forget to drag some object to cube_pfb var.

Comment
Add comment · Show 3 · 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 conflictbliz · Nov 15, 2011 at 11:16 PM 0
Share

maybe thats why its messing up i thought the cube pfb was where you place the prefab to spawn

avatar image conflictbliz · Nov 17, 2011 at 12:10 AM 0
Share

i original wanted it to work only with 1 script and i wanted it to spawn without a rigidbody i didn't say i wanted a script that would take away the rigidbody but thank you anyways i can use it for other uses.

avatar image Mox.du · Nov 17, 2011 at 01:16 AM 0
Share

These are your words, and again interesting - "but i am not sure how to take the rigidbody away inside the script." Don't bother to answer cause this is it from me.

avatar image
0

Answer by conflictbliz · Nov 15, 2011 at 11:37 PM

thank you for both of your scripts they both work but i find that Aldon's script works the best.

Comment
Add comment · Show 1 · 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 Mox.du · Nov 16, 2011 at 02:42 PM 0
Share

Well that's interesting because aldonaletto's scripts does not do what you initially wanted, which is removing rigidbody from the object by script.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Material has been destroyed 1 Answer

[Solved]How to destroy a turret in Tower Defense game 2 Answers

Dissolve object on Gameobject Destory 1 Answer

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

Melle script, enemy destroyed without mouse command once in range. 1 Answer


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