• 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
Question by Musclegunner · Apr 26, 2011 at 07:50 PM · javascriptinstantiaterandomclone

Instantiating a random dropped consumable item from many cloned objects

My goal is to have this script drop a random consumable item (2 or 3 objects) to drop after the 5 second time limit. I can't get each cloned object to drop a random item (or one at all for that matter). The issues I run into is either the parent object will turn into an item or no 'clones' will be instantiated and an object just spawns right on the parent. and gets a tad bit glitchy because of two objects colliding on the same world space. Please help out asap if you can! :D

#pragma strict
#pragma implicit
#pragma downcast
var prefab : Rigidbody;
var speed = 15;
var numberOfObjects = 15;
var radius = 1;
function Update()
{
    if(Input.GetButtonDown("Fire1"))
    {
        LaunchingProjectile();
    }
 }
function LaunchingProjectile()
{
    for (i = 0; i < numberOfObjects; i++)
    {
        var angle = i * Mathf.PI * 2 / numberOfObjects;
        var position = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle))     * radius;
        var clone : Rigidbody = Instantiate(prefab, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
        clone.transform.parent = transform;
        clone.transform.localPosition = position;
        Destroy(clone.gameObject, 5);
    }
}

Comment

People who like this

0 Show 1
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 HailShock1 · Jul 08, 2014 at 12:54 PM 0
Share

Musclegunner...Could you please share how you got random items to drop from cloned objects when they are destroyed as I am attempting to do this and am having a bugger of time. Thank you in advance.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by AngryOldMan · Apr 26, 2011 at 07:57 PM

this line

var clone : Rigidbody = Instantiate(prefab, transform.position, transform.rotation);

instead add

var offset : Vector3 = new Vector3 (1,1,1);

var clone : Rigidbody = Instantiate(prefab, transform.position + offset, transform.rotation);

then alter the offset variable so that the prefab appears in the right position

Comment

People who like this

0 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 Musclegunner · Apr 26, 2011 at 08:17 PM 0
Share

Ok, but now how do I get a random item to drop in place of the cloned object after it's destroyed?

avatar image Musclegunner · Apr 26, 2011 at 10:00 PM 0
Share

NVM I figured out what was wrong, but thanks for the help

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

1 Person is following this question.

avatar image

Related Questions

How to instantiate 2D object at fixed location? 1 Answer

Code only affects first clone? 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Object reference not set to an instance of an object 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