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 joedartjr · Jan 07, 2014 at 04:37 AM · instantiateprefabobjectrandomlocation

Instantiate a random prefab at an objects location

I wrote a script to instantiate a prefab in place of a destroyed object and it works just fine, but now I want that same code to randomly choose from one of four prefabs. I would prefer to not define the prefabs in the script itself, and leave it to be defined through public GameObject's, just like it already is. I need the script to stay as universal as possible so it can be used on all destructible objects, only requiring me to replace the prefabs each time it's added to a new object. I'm just drawing a blank, I know it's a simple solution, I just can't think of how to do it for the life of me. Thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class Destructible : MonoBehaviour {
 
     public GameObject debrisPrefab;
     public GameObject debrisPrefab1;
     public GameObject debrisPrefab2;
     public GameObject debrisPrefab3;
     public float destroyImpact = 20.0f;
 
     void OnCollisionEnter( Collision collision ) {
 
         if( collision.relativeVelocity.magnitude > destroyImpact ) {
         DestroyMe();
         }
     }
 
     void DestroyMe() {
             if(debrisPrefab) {
             Instantiate(debrisPrefab, transform.position, transform.rotation);
             }
         Destroy(gameObject);
         }
 }

An example of the code use is: This code is attached to a brick wall, which is then destroyed. I then have 4 different "destroyed_brick_wall_number" prefabs I want the engine to choose from when replacing the original "brick_wall" object. I currently have it set to use "destroyed_brick_wall_1" in this example and it works fine; but every destroyed wall looks the same. I want them to look different upon destruction, at least a little bit.

Comment
Add comment · Show 6
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 superluigi · Jan 07, 2014 at 04:54 AM 0
Share

Sorry I am away from my pc with the info you need but if I recall correctly you can store your prefabs in an array or you can also use Random.Range. As soon as I boot up the old pc I'll give you an exact answer in the meantime try looking into those two suggestions.

avatar image superluigi superluigi · Jan 07, 2014 at 04:59 AM 0
Share

I used Random.Range to make an enemy switch between player1, player2, and player3 randomly. I'm pretty sure that's what your looking for. Also I'm not entirely sure what you mean by public gameObjects. I assume this is C# talk for defining something through the inspector.

avatar image HappyMoo · Jan 07, 2014 at 05:04 AM 0
Share

Superluigi, don't post comments as answers.

avatar image HappyMoo · Jan 07, 2014 at 06:02 AM 0
Share

Sorry, but "Sorry I am away from my pc with the info you need but if I recall correctly ..." etc. does not constitute an answer. Please watch the tutorial video linked on the right side.

avatar image superluigi · Jan 07, 2014 at 06:06 AM 0
Share

I see what you mean. Next time I'll just try to code it for him.

avatar image joedartjr · Jan 07, 2014 at 06:12 AM 1
Share

It wasn't about coding it for me. You gave a generalized answer to a distinct question. I'd say the 2 snippets of code given by Happy$$anonymous$$oo wasn't "coding it for me" but it got me on the right track, and in the end after tweaking some of my original code it worked fine. I do appreciate the effort, and the fact you even gave a response though.

3 Replies

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

Answer by HappyMoo · Jan 07, 2014 at 04:51 AM

 using System.Collections.Generic;
 
 ..
 
 public List<GameObject> debrisPrefabs;
 
 ..
 
 GameObject fab = debrisPrefabs[Random.Range(0,debrisPrefabs.Count)]
 Instantiate(fab, .....);
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 joedartjr · Jan 07, 2014 at 06:03 AM 0
Share

Thanks a ton! I had to make sure to reference "GameObject fab =" after the "void Destroy$$anonymous$$e()" but before the script called for "fab" in the "if(fab)" statement. Working like a charm now. I'd vote your answer up, but I don't have enough karma; Thank you again for the help.

avatar image
0

Answer by Midwest13 · Jan 07, 2014 at 05:31 AM

You probably want to create an array of GameObjects that you would then use Random.Range to randomly choose one. So something like:

 public GameObject[] debrisPrefabs;

With this to randomly instantiate one:

 Instantiate(debrisPrefabs(Random.Range(0, debrisPrefabs.Length), transform.position, transform.rotation);
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 joedartjr · Jan 07, 2014 at 06:14 AM 0
Share

I appreciate you taking the time to respond, I didn't use this at the moment, but I'll keep it as reference later if I run into another problem. Thanks.

avatar image
0

Answer by NikoBusiness · Jan 07, 2014 at 04:52 AM

it's simple im not going to write the code for you but im going to tell you how to achieve ur goal,so there are 4 prefabs so make a int randomNum = Random.Range(1,4) then make a switch statement if randomNum == 1 then instantiate prefab 1 if 2 the 2nd and so

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 joedartjr · Jan 07, 2014 at 06:06 AM 0
Share

Thank you for responding, I appreciate the effort.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access property of a prefab before Instantiating. 1 Answer

Randomly pick then create prefab 2 Answers

Instantiate Prefab at random 1 Answer

Instantiate Object at random position relative to another object 2 Answers

Tagging object while instating not working. 3 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