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
1
Question by masterarcher · Mar 30, 2011 at 01:46 PM · javascriptprefabspawn

choose 1 of multiple objects to spawn, respawn once its dead.

im trying to create a mario kart style crate that spawns in the middle of the race track.

  • at each spawn point i want to choose between 4 prefabs and spawn it.
  • once its been hit wait 3 seconds to respawn another random box.

this is the script i have

var BulletCratePrefab : Transform; var ExploBulletCratePrefab : Transform; var HitPointCratePrefab : Transform; var NosCratePrefab : Transform;

var CrateInstance = gameObject;

private var spawnPoint : Vector3;

function Update() {

 while(true)
 {
     Spawn();

     while(CrateInstance) //wait until the current crate on spawn point is dead.
     {
         return null;
     }
 }

}

function Spawn () { var index = Random.Range(0 , 3); spawnPoint = this.transform.position;

 // choose which prefab to put on track
 if(index == 0)
 {
     CrateInstance = (gameObject)Instantiate (BulletCratePrefab, spawnPoint, this.transform.rotation);
 }
 else if(index == 1)
 {
     CrateInstance = (gameObject)Instantiate (ExploBulletCratePrefab, spawnPoint, this.transform.rotation);
 }   
 else if(index == 2)
 {
     CrateInstance = (gameObject)Instantiate (HitPointCratePrefab, spawnPoint, this.transform.rotation);
 }
 else if(index == 3)
 {
     CrateInstance = (gameObject)Instantiate (NosCratePrefab, spawnPoint, this.transform.rotation);
 }   

}

the only referrence i had was for c# so im not sure if i can do it the same.

Edit: this is the c# script i have. it selects 1 of xAmount and spawns a target, once its dead it spawns again.

using UnityEngine; using System.Collections;

public class Spawner : MonoBehaviour { public GameObject TargetPrefab; // Set this in inspector to the target prefab. public int RandTimeMin = 3; // Minimum amount of time before spawning enemy. public int RandTimeMax = 10; // Maximum amount of time before spawning enemy. GameObject targetInstance; Transform[] spawnpoints;
public int SpawnNumber = 10; int Spawned = 0; int lastIndex =0; IEnumerator Start() { spawnpoints = GetComponentsInChildren<Transform>();

     while (true &amp;&amp; Spawned &lt; SpawnNumber)
     {
         yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
         Spawn();
         while (targetInstance) // Wait for target to die.
         {
             yield return null;
         }
         yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
     }

     yield return new WaitForSeconds(6);
     Application.LoadLevel(0);
 }

 void Spawn()
 {
      int index = 0;
      Spawned++;

      while(lastIndex == index )
      {
          index = Random.Range(0, spawnpoints.Length - 1);
      }
      var spawnpoint = spawnpoints[index];
      targetInstance = (GameObject)Instantiate(TargetPrefab, spawnpoint.position, spawnpoint.rotation);
 }

}

instead of creating a list of spawnpoints, i thought it would be easier to handle each spawnpoint individually so if its crate is destroyed it automatically deals with it. as you can see i havent translated the c# in to Js and my idea very well.

Comment
Add comment · Show 2
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 demize2010 · Mar 30, 2011 at 02:15 PM 1
Share

gameObject CrateInstance;

should be

var CrateInstance : gameObject;

avatar image masterarcher · Mar 30, 2011 at 03:08 PM 0
Share

thanks i couldnt remember how to do it lol

3 Replies

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

Answer by masterarcher · Mar 30, 2011 at 05:18 PM

i went back to my c# script and realized in my game it doesn't matter its c# so changed it.

this works perfectly.

using UnityEngine; using System.Collections;

public class PowerUp_Script : MonoBehaviour { public GameObject NormBulletCratePrefab; // Set this in inspector to the target prefab. public GameObject ExploBulletCratePrefab; // Set this in inspector to the target prefab. public GameObject HPCratePrefab; // Set this in inspector to the target prefab. public GameObject NosCratePrefab; // Set this in inspector to the target prefab.

 public int RandTimeMin = 3;  // Minimum amount of time before spawning enemy.
 public int RandTimeMax = 5;  // Maximum amount of time before spawning enemy.
 GameObject CrateInstance;

 IEnumerator Start()
 {
     while (true)
     {
         Spawn();
         while (CrateInstance) // Wait for target to die.
         {
             yield return null;
         }
         yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
     }
 }

 void Spawn()
 {
      int index = Random.Range(0, 3);

      //select the box to spawn
      if(index == 0)
      {
         CrateInstance = (GameObject)Instantiate(NormBulletCratePrefab, this.transform.position, this.transform.rotation);
      }
      if(index == 1)
      {
         CrateInstance = (GameObject)Instantiate(ExploBulletCratePrefab, this.transform.position, this.transform.rotation);
      }      
      if(index == 2)
      {
         CrateInstance = (GameObject)Instantiate(HPCratePrefab, this.transform.position, this.transform.rotation);
      }            
      if(index == 3)
      {
         CrateInstance = (GameObject)Instantiate(NosCratePrefab, this.transform.position, this.transform.rotation);
      }        
 }

}

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
1

Answer by Justin Warner · Mar 30, 2011 at 01:57 PM

Well...

I can't really read that script in the Update function, looks like an infinite loop in my opinion... So the game shouldn't even run...

Well.. What I'd do, because this is all I can do to really help you, is say how I'd do it..., so, what I'd do is:

Make an array of availible boxes that can be used...

Now, it's Mario Kart style, so every spot on the map that you want on, their should be one.

So, you put in "place holders" these can be with empty game objects, or with an actual image that isn't rendered (So you can actively see it in the editor if you sway that way =P), and so you loop through with a for:each, and you'd grab a random number, and with the number, you'd just spawn all the items in the function awake.

Now if someone goes over and takes one of the boxes... You have in the update function, to keep track of how many was TO START, so now, you have let's say 30 boxes... So, if the number of of current objects (You can loop this in the Update), is less than the start, then you find where it is (Maybe use a List?), and add it there...

This should work for everything I believe...

Hope this helps a little, even though it's not directly related to you're code...

Comment
Add comment · Show 4 · 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 masterarcher · Mar 30, 2011 at 02:57 PM 0
Share

see updated question

avatar image Justin Warner · Mar 30, 2011 at 03:08 PM 0
Share

So you're just trying to convert C to Java? Well... from reading the script, it sounds like it doesn't spawn a box on EVERY spot... only a random one? For that case, add all the place holders in to an array or List, randomly find a number, add box at the game object's location... That should work really well and be like, ten lines of code maybe?

avatar image masterarcher · Mar 30, 2011 at 03:47 PM 0
Share

i wrote the c# script for a different game.

the script is going to be on a spawnpoint prefab, so i can position it on the track where i want.

each point will select one of the 4 crates, and spawn it. once its destroys it does the same.

avatar image Justin Warner · Mar 30, 2011 at 09:59 PM 0
Share

Unity allows C# scripts too as well =).

avatar image
1

Answer by Statement · Mar 30, 2011 at 05:46 PM

Just use an array.

var powerups : GameObject[]; var powerup : GameObject; var minDelay : float = 3; var maxDelay : float = 5;

Spawn();

function Update() { if (!powerup && !IsInvoking("Spawn")) Invoke("Spawn", Random.Range(minDelay, maxDelay)) }

function Spawn() { var prefab = powerups[Random.Range(0, powerups.Length)]; powerup = Instantiate(prefab, 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 Statement · Mar 30, 2011 at 05:51 PM 0
Share

You're not supposed to set powerup in the inspector. I just kept it public so you can easier see what powerup is active (although you should probably be able to see it in the scene easily). It should probably be private or @HideInInspector.

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

No one has followed this question yet.

Related Questions

How do I spawn enemies based on a timer? 1 Answer

Destroy and Spawn an Enemy 1 Answer

Monster Spawner 3 Answers

Prefab to spawn on right spot 1 Answer

Spawn object in random areas 2 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