Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Momin1 · Aug 21, 2016 at 05:45 PM · c#scripting probleminstantiatearraysif-statements

how to instantiate an object from an array above the previously instantiated object from the same array?

Hi everyone...I have searched alot but unable to find a solution so plz help.Basically what i am trying to do is as you can see in the simplified script that i have made an array(it have three items an each is parent of its child objects) and then instantiating these items randomly.what i want is that the first instantiate should occur above an object already in the scene but then the second object should instantiate on above the previous instantiated object and the third one above the second instantiate and so on. Now what i think i want is a way to determine if intantiate(if this is not possible then we can put instantiate in a seperate function and check if that happened or no but i dont understand how to put either of these two checks in if condition) have occured or no,that is the condition for if statement,if its yes then i need a reference to the last instantiation that ocurred so i can put it equal to lastRedgeSet.plz help and also suggest a better way of doing this if any`public class Pcg : MonoBehaviour {

 public GameObject[] RedgeSets;
 private GameObject LastRedgeSet;
 public GameObject FixedRedges;
 private Object myist;


 void Start () {
     LastRedgeSet = FixedRedges;
     }
 

 void Update () {


     // if(instantiate happened or no Condition here){
     //LatRedgeSet = (refrence to last instantiated object required  here) }


     Vector3 LastRedgeSetPos = LastRedgeSet.transform.position;
     Vector3 addheight = new Vector3 (0, 5, 0);

     if (Input.GetKeyDown (KeyCode.Z)) {
         Instantiate (RedgeSets [UnityEngine.Random.Range (0, 3)], LastRedgeSetPos + addheight, Quaternion.identity);
     }
         
 
     }`
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

1 Reply

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

Answer by JedBeryll · Aug 21, 2016 at 06:09 PM

Just have a Transform variable that by default points to the "object already in the scene". You assign this transform as parent of a newly created instance and change the Transform reference to this new object. Next time when you create an instance the previous object will be parent. So all you have to do is always set the saved transform as parent.

Comment
Add comment · Show 3 · 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 Momin1 · Aug 22, 2016 at 12:16 PM 0
Share

thanks for your suggestion but i am not able to clearly understand what you are trying to say if you could just explain a little more

avatar image JedBeryll Momin1 · Aug 22, 2016 at 01:17 PM 0
Share
 public GameObject[] RedgeSets;
     private GameObject LastRedgeSet;
     public GameObject FixedRedges;
     private Object myist;
 
     private Transform lastObject;
     //make this variable public and set it to the default object in the inspector
     //or assign it in Start() or however you want
 
 
     void Start () {
         LastRedgeSet = FixedRedges;
     }
 
     void Update () {
         Vector3 LastRedgeSetPos = LastRedgeSet.transform.position;
         Vector3 addheight = new Vector3 (0, 5, 0);
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Z)) {
             GameObject newObject = Instantiate (RedgeSets [UnityEngine.Random.Range (0, 3)], LastRedgeSetPos + addheight, Quaternion.identity) as GameObject;
             Transform t = newObject.transform;
             t.parent = lastObject;
             lastObject = t;
             //this way the newly created object will be a child of the default object in the map
             //after the first new object has been created, every other object will be a child of the previous object
         }
     }
avatar image Momin1 JedBeryll · Aug 22, 2016 at 06:14 PM 0
Share

thanks alot man u r great...this seems to work perfectly

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a Prefab along the surface of a sphere. C# 0 Answers

How can I in-script create gameobject from prefab? 2 Answers

Integer arrays not comparing properly. 3 Answers

Problems with if-statements (C#) 1 Answer

Error with Instantiate after build 0 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