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 Infinite_Gamer · Jul 02, 2014 at 08:49 PM · c#spawnrandom.range

(C#) Help with spawning a random GameObject

*Edited

Hello Unity community. I need help with spawning a gameobject.

So this is What I want to do. I want to have a random number choose 1 gameobject to spawn then I want to spawn just that One gameobject.

Here is my script *Edited

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(MobHealth))]
 public class MobDrop : MonoBehaviour 
 {
 
     public GameObject MobGameObject;
     private int DropCounter = 0;
 
     //Different things the mob can drop
     public GameObject[] Drops;
 
     public int RandomNumber;
 
     //Get the MobHealth script
     private MobHealth Mob;
 
     void Start () 
     {
         Mob = MobGameObject.GetComponent<MobHealth>();
     }
 
     void Update () 
     {
         if(Mob.IsDead == true && DropCounter == 0)
         {
             DropTheMobDrop();
         }
     }
 
     void DropTheMobDrop()
     {
         DropCounter ++;
         RandomNumber = Random.Range(0,2);
 
         var MobDrop = Instantiate(Drops[RandomNumber], new Vector3(transform.position.x, transform.position.y, 0.0f), Quaternion.identity) as GameObject;
         MobDrop.name = Drops[RandomNumber].name;
     }
 }


  • this seems to work. Is it the best way to do it?

Thanks for any help :D

Comment
Add comment · Show 7
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 rutter · Jul 02, 2014 at 06:10 PM 0
Share

You've set up an infinite loop:

 while(true) { ... }

Loops are usually used to make a thing happen more than once. If you want it to happen just once, why is it in a loop?

avatar image robertbu · Jul 02, 2014 at 09:11 PM 0
Share

And if you get rid of the 'while' loop, I don't see any reason in the current code to make it a Coroutine. Change IEnumerator to void, and redo line 27 to be:

 DropThe$$anonymous$$obDrop();

Also if I understand correctly what you are doing, you will need to reinitialize '$$anonymous$$ob' to the '$$anonymous$$obDrop', and you will need to change line 37 to:

 var $$anonymous$$obDrop = Instantiate(Drops[RandomNumber], new Vector3(transform.position.x, transform.position.y, 0.0f), Quaternion.identity) as GameObject;
avatar image adsamcik · Jul 04, 2014 at 08:26 AM 0
Share

You don't despawn the mob anywhere, so every frame when Update is called it checks if the mob is dead, than it finds out it is and drops something.

avatar image Infinite_Gamer · Jul 04, 2014 at 02:42 PM 0
Share

I tired to make it despawn when it dies but that makes it not spawn anything.

avatar image flaviusxvii · Jul 04, 2014 at 03:45 PM 0
Share

There is a ton of confusing stuff going on here. Are you attaching this component to the mob? I see you are requiring $$anonymous$$obHealth, which would make sense. But then you are taking the $$anonymous$$obHealth component from another GameObject $$anonymous$$obGameObject. That's not going to work the way you expect.

Also.. you variable names should start with lower-case letters.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tebandesade · Jul 02, 2014 at 08:59 PM

Hi! You might want to check out http://docs.unity3d.com/Manual/RandomNumbers.html

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 Infinite_Gamer · Jul 04, 2014 at 08:21 AM 0
Share

Thanks I will check it out.

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

6 People are following this question.

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

Related Questions

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Not repeating random 2 Answers

(C#)Place GameObject from Mouse 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