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 LightSource · Apr 27, 2014 at 08:35 PM · arrayobjectjava

Adding instantiated objects to arrays

Okay, so I have a script that spawns cubes. I attempted to create code that adds a new instantiated cube to an array when you collide with one of the spawned bodies. My script currently spawns the cube but does not add it to the array. Any help is appreciated, thanks.

Spawn Manager:

 #pragma strict
 
 var objectController : objectController;
 
 function Start () {
 var gameManager : GameObject;
 gameManager = GameObject.Find("_gameManager");
 objectController = gameManager.GetComponent("objectController");
 }
 
 function OnTriggerEnter () {
 objectController.addObject();
 Destroy(gameObject);
 }

Cube Spawner:

pragma strict

 var spawnedBody : GameObject;
 var radius : float = 20.0;
 var numberOfSpawns : int = 20;
 var spawnForever = false;
 
 
 function Start () {
     if (!spawnForever) {
         for (var i : int = 0; i <= numberOfSpawns; i++) {
         Instantiate(spawnedBody, Random.insideUnitSphere * radius + Vector3(-15,15,-15), transform.rotation);
         }
     }
 }
 
 function Update () {
     if (spawnForever) {
     Instantiate(spawnedBody, Random.insideUnitSphere  * radius + Vector3(-15,15,-15), transform.rotation);
     }
 
 }

Object Controller:

 #pragma strict
 
 var bodyString : Transform[];
 var prefab : Transform;
 var position : int = 0;
 var initalNumber : int = 5;
 
 function Start () {
 initalSpawn(initalNumber);
 }
 
 function initalSpawn (number : int) : IEnumerator {
      for (var x = 0; x < number; x++) {
      addObject();
      yield WaitForSeconds (.2);
      }
 }
 
 function addObject () {
 bodyString[position] = Instantiate(prefab, transform.position, transform.rotation);
 bodyString[position].GetComponent(followObject).changePosition(position);
 position++;
 }
Comment
Add comment · Show 1
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 ransomink · Apr 27, 2014 at 10:26 PM 0
Share

I don't see where you're trying to add the cube to an array. I don't even see an Array () in your scripts?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ransomink · Apr 27, 2014 at 11:00 PM

  1. Check if the player has collided with a spawned body

  2. If so, spawn a new cube

  3. After the cube is spawned, add it to the array

spawnPoint on the CubeSpawner script is used as the position of the instantiate object. This was for test reasons; Obviously you'll use your own Vector3.

CubeSpawner:

 #pragma strict
 
 // STATIC VARIABLES //
 public static var cs : CubeSpawner;// Reference to the CubeSpawner script
 
 // INSPECTOR VARIABLES //
 public var cube : GameObject;// GameObject used to spawn
 public var spawnPoint : Vector2;// Spawn position of the gameobject
 public var cubeArray : Array;// Array holding all spawned cubes
 
 // Used for initialization
 function Start ()
 {
     cs = this;// Set cs as the reference to the CubeSpawner script
     
     // Create the cubeArray
     cubeArray = Array ();
 }
 
 // Spawn a new cube
 public function SpawnCube ()
 {
     var newCube : GameObject;
     newCube = Instantiate ( cube, spawnPoint, transform.rotation );
     
     // Add the spawned cube to an array
     cubeArray.Push ( newCube );
     Debug.Log ( cubeArray );
 }

spawnedBody on the CheckTrigger script should be the spawned bodies you spoke of in your game.

CheckTrigger:

 #pragma strict
 
 // Check when a gameobject has entered the trigger
 function OnTriggerEnter ( col : Collider ) 
 {
     // If the collided object is a spawned body...
     if ( col.gameObject == spawnedBody )
     {
         // ... spawn a new cube, add it to the array, and destroy the collided gameobject
         CubeSpawner.cs.SpawnCube ();
         //Destroy ( col.gameObject );
     }
 }


If you have multiple spawned bodies, there are 2 methods (I can think of now) that you can use.

Method #1:

Put all spawned bodies in an array. Inside of the trigger function, check through each gameobject in that array, and compare that gameobject to the collided gameobject. If they match, then spawn a new cube.

Method #2:

Give each spawned body a tag called 'SpawnedBody' (without quotes). Inside of the trigger function, compare the collided gameobjects tag to the spawned bodies tag. If they match, spawn a new cube. This method is cleaner and seems easier to do than method 1.

p.s. Do not use arrays, specially in Unityscript; Instead, use a List. Here are some great references for learning what list are and how to use them.

http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html

http://answers.unity3d.com/questions/327065/explanation-on-how-to-use-list-in-unityscript.html

http://msdn.microsoft.com/en-us/library/d9hw1as6%28v=vs.90%29.aspx

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

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

21 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

Related Questions

Add custom properties to Objects 1 Answer

destroying object upon collision 1 Answer

Is there any option to put objects together? 0 Answers

Keep track of objects within a trigger. 0 Answers

How can i delete an item from an array after being used? (So, it won't be repeated) 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