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 Geta-Ve · Jul 24, 2012 at 08:23 AM · instantiateprefabarrayobjectsmultiple

Instantiate an array of prefabs?

Hey there. So basically what I am trying to do is have multiple objects (ie, rock, tower, etc) randomly throughout my scene. Gameplay is as follows:

https://www.youtube.com/watch?v=mJ8NFJcs4Uw

Objects (obstacles) will hurdle towards you while you move your character onscreen left and right to dodge/avoid getting hit.

Ideally I'd like the system to be expandable so that I can add different obstacles down the line, through the editor, with as little programming as possible. As well as being able to define their spawn 'patterns' (time, size, etc).

Lastly, and while this isn't necessarily part of this question, if you'd be able to point me in the direction of having the instantiated objects, NOT spawn on top of each other, that would be super awesome!

Here is the code that I have thus far. What I was attempting to do was to create an 'obstacles' array, whereupon I would store all the objects that I'd like the game to spawn randomly. The Random spawning happens down on the Instantiate line. While this system works for a single game object (without using an array I mean) I simply can't wrap my head around multiple objects.

Thank you for any help given!! Also, I apologize for my incredible density, I am quite new, obviously, and am still learning all these things. I am probably using arrays totally wrong or something. :P

Here is a PasteBin link, since the UA code snippet is a little hard to read. (lemme know if this doesnt work)

http://pastebin.com/zzvq6Vaf

TowerCreation Script

#pragma strict

/ Spawn & Reuse Objects - Originally created by 'save' (of Unity Answers) Updated: 2012-07-18 /

enum PLAYERAXIS { x,y,z }

var obstacles : GameObject[]; //Obstacles var obstacles1; var obstacles2; var obstacles3;

var playerTransform : Transform; //Player's transform (we use this to set position of the spawn area) var totalSpawnObjects : int = 50; //The amount of objects that is allowed in the scene var spawnPause : float = .2; //The pause between spawns var spawnAreaPosition : Vector3 = new Vector3(0,0,0); //The position of the spawn area var spawnAreaSize : Vector3 = new Vector3(200,0,50); //The size of the spawn area (we then randomly spawn inside this area in X, Y and Z) var followPlayerOnAxisDistance : PLAYERAXIS = PLAYERAXIS.z; //The axis the spawn area follow the player in distance var distanceFromPlayer : int = 200; //The distance of the spawn are to the player

private var spawnTimeScheduler : float = .0; private var spawnPointer : int = 0;

// Static variables for cached components of the objects below (we use this to access the spawned objects outside the script easily) static var objects : GameObject[]; static var objectsTransform : Transform[]; static var objectsRenderer : Renderer[];

function Start () { objects = new GameObject[totalSpawnObjects]; objectsTransform = new Transform[totalSpawnObjects]; objectsRenderer = new Renderer[totalSpawnObjects];

 if (playerTransform==null) playerTransform = GameObject.FindGameObjectWithTag("Player").transform;

}

function Update () { if (Time.time>=spawnTimeScheduler) Spawn(); }

function Spawn () { //Position the spawn area spawnAreaPosition = playerTransform.position;

 switch (followPlayerOnAxisDistance) 
 {
     case PLAYERAXIS.x: spawnAreaPosition.x += distanceFromPlayer; break;
     case PLAYERAXIS.y: spawnAreaPosition.y += distanceFromPlayer; break;
     case PLAYERAXIS.z: spawnAreaPosition.z += distanceFromPlayer; break;
 }

 //Bake the spawn position into a variable
 var spawnPosition : Vector3 = Vector3
     (
         spawnAreaPosition.x - Random.Range(-spawnAreaSize.x / 2, spawnAreaSize.x / 2),
         spawnAreaPosition.y - Random.Range(-spawnAreaSize.y / 2, spawnAreaSize.y / 2),
         spawnAreaPosition.z - Random.Range(-spawnAreaSize.z / 2, spawnAreaSize.z / 2)
     );

 //Spawn new object
 if (objects[spawnPointer]!=null) 
 {
     //Reuse the object
     objectsTransform[spawnPointer].position = spawnPosition;
 }
  
     else 
     {
         //Instantiate the object
         var thisObject : GameObject = Instantiate(obstacles[0,1,2], spawnPosition, Quaternion.identity);
         
         CurrentSpawnedComponentCache(spawnPointer, thisObject.gameObject, thisObject.transform, thisObject.renderer);            
     }
     
   
 if (spawnPointer++ >= totalSpawnObjects - 1) spawnPointer = 0;
 spawnTimeScheduler = Time.time + spawnPause;

}

//Cache the components of spawned objects function CurrentSpawnedComponentCache (i : int, g : GameObject, t : Transform, r : Renderer) { objects[i] = g; objectsTransform[i] = t; objectsRenderer[i] = r; }

//Draw the spawn area in Scene view function OnDrawGizmos () { Gizmos.color = Color.yellow; Gizmos.DrawWireCube (spawnAreaPosition, spawnAreaSize); }

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple object prefab instantiates too far down 0 Answers

Array of prefabs with different sizes stick to each other 0 Answers

Assigning Instantiated Tiles to Array 1 Answer

Instantiate 3 guiTextures at the same 1 Answer

Prefabs instantiated from an array are keeping their public int value 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