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 matta9001 · Mar 08, 2014 at 11:46 PM · javascriptinstantiatecreate

A bit complicated instantiate question

So i have 3 meteors, they are all disabled. I want to make it so that with a script i can call a function, that will spawn a copy of the disabled meteor, undisable it, let it fall across the screen, then destroy the gameobject. So that at any time, i can call the function and have a meteor fall down. I also want to randomize which one falls down so that constantly a random meteor falls down every few seconds. So it creates the effect that you are flying through a whole lot of meteors, but its infinite.

Comment
Add comment · Show 3
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 Nick4 · Mar 08, 2014 at 11:53 PM 1
Share

You don't need to keep enabling and disabling meteors. Just make a meteor prefab and define a list of possible spawn locations. Then choose one once in a few seconds randomly using Random.Range() function.

avatar image matta9001 · Mar 09, 2014 at 12:27 AM 0
Share

I'm not that good enough to write a script off of that information, do you $$anonymous$$d writing like an example script (javascript please), showing me how i can do it, i can do it myself from there

avatar image superluigi · Mar 09, 2014 at 12:31 AM 0
Share

Ok I'll help you out, but it's really important that you learn it.

2 Replies

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

Answer by thornekey · Mar 09, 2014 at 12:00 AM

like Nick4 said, you can define a list of spawn places, if you create an array and hold the spawn places in there you can randomly choose different meteor prefabs (assuming u want some uniqueness) and then randomly spawn the amount, and random range location of them.

For example (this wont be 100% accurate nor complete but an outline)

 private bool CheckForMeteorPrefabs() {
         if (meteorPrefab.Length > 0)
             return true;
         else
             return false;
     }
     
     private bool CheckForSpawnPoints() {
         if (spawnPoints.Length > 0)
             return true;
         else
             return false;
     }
     
     //generate a list of available spawnpoints that do not have any meteors childed to it
     private GameObject[] AvailableSpawnPoints() {
         List<GameObject> gos = new List<GameObject>();
         
         for(int cnt = 0; cnt < spawnPoints.Length; cnt++) {
             if (spawnPoints[cnt].transform.childCount == 0) {
                 Debug.Log("**meteor spawn point available**");
                 gos.Add(spawnPoints[cnt]);
         
             }
         }
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 superluigi · Mar 08, 2014 at 11:59 PM

Seems like you already have it figured out. You're going to want to create 3 variables and store your meteors there. You will then want to "Instantiate" using "Random.Range" to randomize between your 3 meteor variables. You can then start an "InvokeRepeating" loop to "Instantiate" every whatever timespan you choose. The words in "" are the keywords you want to check out. I am willing to help you out with this and help you write and explain anything you need for as long as you need me to. However, I am not willing to write the code for you as that won't really help you at all.

 #pragma strict
 var m1 : GameObject;
 var m2 : GameObject;
 var m3 : GameObject;
 
 InvokeRepeating("SpawnMeteors", 2, 2);
 
 
 function SpawnMeteors()
 {
     var meteors : Array = Array(m1,m2,m3);
     var position : Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, 0);
     Instantiate(meteors[Random.Range(0, meteors.length)], position, m1.transform.rotation);
 }

This is tested an confirmed to work. If however you run into any problems let me know.

Comment
Add comment · Show 5 · 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 superluigi · Mar 09, 2014 at 01:15 AM 0
Share

Also if you need me to explain any of the lines, or all of them, let me know. I'll tell you what each of them does.

avatar image matta9001 · Mar 09, 2014 at 02:48 AM 0
Share

Ok i have found a few problems but there is no errors. Ok so one problem is that when it first starts the 3 meteors fall down on the screen making it impossible to avoid. If i disable the meteors then it just spawns more disabled meteors. Also it never deletes the copies. Plus they don't spawn in the right spot. But i think i can fix that last problem on my own its just the first meteors spawn and fall and i can't really change that because if i disable them then the script makes more disabled meteors

avatar image matta9001 · Mar 09, 2014 at 04:00 AM 0
Share

And dude, thank you so much. I could have never written this script by myself and i want to be able to do so someday. I'm 13 and i have a lot of time to learn :)

avatar image matta9001 · Mar 09, 2014 at 06:19 AM 0
Share

How do i make it so that after some time they get deleted?

avatar image matta9001 · Mar 09, 2014 at 06:35 AM 0
Share

Or mabye i could make a box collider with a function to break the gameobject that enters it

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

23 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 avatar image

Related Questions

Spaceing out objects 2 Answers

Best way to respawn car? 0 Answers

How to activate a button? 1 Answer

Instantiate problem in a RTS game 0 Answers

How to add random force/Rotation to bulletEject. 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