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 Nsingh13 · May 05, 2015 at 12:27 AM · gameobjectinstantiategetcomponentspawningpooling

Get components of multiple instantiated objects?

Hi, I have a script where I randomly spawn a given array of objects at a random time between 2 values. What I have been stuck on for the past hour is trying to get the transform component of the object I just instantiated, so I can reset the position after a certain time. Thus, making a pool for all my objects.

The code is as follows:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BugSpawn : MonoBehaviour
 {
 
         public GameObject[] objlist;
         public float spawnmin = 1f;
         public float spawnmax = 2f;
 
         public bool firsttime;
         void Start ()
         {
                 firsttime = true;
 
                 Spawn ();
         }
 
         void Spawn ()
         {
                 if (firsttime == false) {
                         Instantiate (objlist [Random.Range (0, objlist.GetLength (0))], transform.position, Quaternion.identity);
                 }
 
                 Invoke ("Spawn", Random.Range (spawnmin, spawnmax));
 
 
                 firsttime = false;
         }
 }

Thanks! any help is greatly appreciated :)

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

2 Replies

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

Answer by DiegoSLTS · May 05, 2015 at 02:24 AM

"Instantiate" returns a reference to the object created. You can cast that reference to GameObject and then get the Transform component like you'll do with any other GameObject.

 if (firsttime == false) {
     GameObject instance = (GameObject)Instantiate (objlist [Random.Range (0, objlist.GetLength (0))], transform.position, Quaternion.identity);
     // do something with instance.transform
 }

http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

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 Nsingh13 · May 05, 2015 at 04:36 AM 0
Share

Hi there! firstly, thanks for the reply! I'm sorry that i hadn't mentioned this before but I already tried that and for some reason it doesn't recognise instance as a variable. I can't seem to put my finger on what's wrong.

avatar image Addyarb · May 05, 2015 at 05:06 AM 0
Share

Can you post the errors you're getting? There's a few other ways, like making a list/array. I'm interested to see what's wrong with your code at this point, though.

avatar image fafase · May 05, 2015 at 06:05 AM 0
Share

you probably have this:

   if (firsttime == false) {
        GameObject instance = (GameObject)  Instantiate (prefab);
    }
    instance.transform.position = new Vector3();

your problem is that instance is local to the if statement and does not exist outside of it. You either need to perform the action within the if as shown in the answer or make the instance variable more global by declaring it outside the if statement.

avatar image Nsingh13 · May 14, 2015 at 02:02 AM 0
Share

Hi again! sorry for the late reply, I've been really busy with school. For some reason the code above started to work. Thanks for all your help guys! And thanks fafase for the global tip.

avatar image
1

Answer by Addyarb · May 05, 2015 at 02:26 AM

There's a different method for instantiating objects and then accessing them later. It looks something like this

 GameObject nameOfNewGameObject = Instantiate (objlist[Random.Range(0,objlist.GetLength(0))],tranform.position,Quaternion.identity) as GameObject;
 
 nameOfNewGameObject.transform.position = new Vector3 (1,2,3);
 
 



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 fafase · May 05, 2015 at 06:01 AM 0
Share

it is not different, it is just the cast that is different

  var instance = (Type)action();

if action returns an object that cannot be cast to Type, the compiler crashes.

 var instance = action() as Type;


if action returns an object that cannot be cast to Type, instance is null but no crash.

This is just a matter of do you need the object to cast or do you just want to try to cast.

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

20 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

Related Questions

Will Instantiating prefab with many components slow my game? 1 Answer

Cannot find the length of an array 3 Answers

Only change a variable on the instaniated object not the prefab. 0 Answers

Accessing instantiated GOs nested in prefabs with GetComponent 4 Answers

Limiting respawns on scene 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