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 Dollar-Fish · Jul 01, 2015 at 09:53 AM · objectunity 2dspawninglocationcreate

Spawn A Set Amount of Objects at Set Location

I have been all over the internet trying to figure this out. I have 13 fruit, and I want them to spawn where the watermelon is. I want the watermelon to have a 12/86 chance of spawning, the strawberry 11/86 chance of spawning, and etc. I want to make it so every 2 seconds it spawns an object at that set location(where the watermelon is)and the fruit will fall in case you where wonder. Thank you in advance. Script is appreciated. alt text

fruitt.png (134.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HenryChinaski · Jul 10, 2015 at 02:12 AM

 C#
 
 public GameObject spawnPoint;
 
 public GameObject watermelon;
 public GameObject strawberry;
 public GameObject apple;
 
 etc..
 
 void spawnFruit()
 {
 int whichFruit = Random.Range(1,100);
 GameObject thatFruit;
 
 thatFruit=apple;
 if (whichFruit > 10) thatFruit=strawberry;
 if (whichFruit > 20) thatFruit=watermelon;
 etc.
 
 Instantiate(thatFruit, spawnPoint, Quaternion.identity);
 
 }

Something like that should work. Just assign a gameObject where you want the spawn point to be and your fruits as gameObjects. In game you just have to call the spawnFruit function.

Comment
Add comment · Show 12 · 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 Dollar-Fish · Jul 14, 2015 at 03:00 PM 0
Share

It didn't really work. This is my script and I want it so in 5 seconds it spawns a fruit every second. It creates a random number every second. I am having trouble spawning the fruit too. Is there something in my script I can fix? Thank you in advance.

using UnityEngine; using System.Collections;

public class spawnFruit : $$anonymous$$onoBehaviour {

 public GameObject spawnPoint;

 public GameObject watermelon;
 public GameObject strawberry;
 public GameObject grape;
 public GameObject banana;
 public GameObject orange;
 public GameObject tomato;
 public GameObject pineapple;
 public GameObject apple;
 public GameObject blueberries;
 public GameObject raspberries;
 public GameObject pear;
 public GameObject blackberries;
 public GameObject lemon;

 public int number = 0;
 
 void Start()
 {
     InvokeRepeating("number", 5, 1);
     number = Random.Range (1,87);
 }

 void spawn_fruit()
 {
 
     if (number >= 1)
     if (12 >= number)
     {

     Instantiate(watermelon, Vector2(0.18,4.79), Quaternion.identity);

     }
 }

}

avatar image HenryChinaski · Jul 14, 2015 at 03:17 PM 0
Share
  1. What do you do with invokeRepeating? Do you mean: "InvokeRepeating("spawn_fruit", 5, 1);"?

  2. in your spawn_fruit void, you are saying that if number is 1 or larger (so every possible number which makes it not relevant) then look if number is smaller then 12. Just write if (number > 0) then ..., (number > 11) then ..., (number > 21) then ..., and go ahead like that. $$anonymous$$uch easier if you ask me.

avatar image HenryChinaski · Jul 14, 2015 at 03:19 PM 0
Share

Also, if your script does not work. Never ever ever say "it does not work" without describing WHAT EXACTLY does not work. What happens on screen? If you press play? Are there Errors or Warnings?

avatar image Dollar-Fish · Jul 14, 2015 at 09:19 PM 0
Share

I hope that this will work but I have a parsing error at line 118 and I don't know how to fix it. Besides that, will this code work possibly? Thank you for helping me out.

using UnityEngine; using System.Collections;

public class spawnFruit : $$anonymous$$onoBehaviour {

 public GameObject spawnPoint;

 public GameObject watermelon;
 public GameObject strawberry;
 public GameObject grape;
 public GameObject banana;
 public GameObject orange;
 public GameObject tomato;
 public GameObject pineapple;
 public GameObject apple;
 public GameObject blueberries;
 public GameObject raspberries;
 public GameObject pear;
 public GameObject blackberries;
 public GameObject lemon;

 public int number = 0;
 
 void Start()
 {
     InvokeRepeating("spawn_fruit", 5, 1);
     number = Random.Range (1,87);
 }

 void spawn_fruit()
 {
 
     if (number > 0)
     {

     Instantiate(watermelon, Vector2(0.18,4.79), Quaternion.identity);

     }

     if (number > 12)
     {
         
         Instantiate(strawberry, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 23)
     {
         
         Instantiate(grape, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 33)
     {
         
         Instantiate(banana, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 42)
     {
         
         Instantiate(orange, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 50)
     {
         
         Instantiate(tomato, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 58)
     {
         
         Instantiate(pineapple, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 65)
     {
         
         Instantiate(apple, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 71)
     {
         
         Instantiate(blueberries, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 76)
     {
         
         Instantiate(raspberries, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 80)
     {
         
         Instantiate(pear, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     if (number > 83)
     {
         
         Instantiate(blackberries, Vector2(0.18,4.79), Quaternion.identity);
         
     }

     else if (number > 85)
     {

         Instantiate(lemon, Vector2(0.18,4.79), Quaternion.identity);

     }
 }
avatar image HenryChinaski · Jul 15, 2015 at 09:28 PM 0
Share

Your missing the last }.

Every { needs a } somewhere in the script, otherwise you will get a parsing error. You opened the void spawn_fruit but didnt close it at the end.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Create an Object at a X,Y,Z location on the fly 1 Answer

Spawn GameObject Above and Below GameObject 0 Answers

Default object placement location. 0 Answers

How I can create a Javascript object in a unity Script? 1 Answer

move object when player reach trigger 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