Search Unity

How to get the position of the last randomly instantiated prefab in a scene?

Discussion in 'General Discussion' started by Junglerally, Dec 3, 2020.

  1. Junglerally

    Junglerally

    Joined:
    May 4, 2020
    Posts:
    14
    Sorry if thetitle was a bit hard to understand. Ok, I have this script that will randomly spawn 15 platforms within a randomly chosen offset. I want to get the position of the last platform instantiated so that I can store it in a variable such as lastPos or something, then add that to the Vector3 that determines the position of the platform. Here is my script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Generator : MonoBehaviour
    6. {
    7.     public GameObject platform;
    8.     public bool levelGenerated = false;
    9.     public int platformLimit;
    10.  
    11.     private float yDifference = 3.5f;
    12.     private float xDifference = 10f;
    13.  
    14.     private float generateDelay = 0.1f;
    15.  
    16.     void Start()
    17.     {
    18.         InvokeRepeating(("GeneratePlatform"), generateDelay, generateDelay);
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(GameObject.FindGameObjectsWithTag("Platform").Length == platformLimit)
    25.         {
    26.             levelGenerated = true;
    27.         }
    28.     }
    29.  
    30.  
    31.     void GeneratePlatform()
    32.     {
    33.         if (levelGenerated == false)
    34.         {
    35.             Instantiate(platform, GenerateRandomPos(), transform.rotation);
    36.         }
    37.     }
    38.  
    39.     private Vector3 GenerateRandomPos()
    40.     {
    41.         float yPos = Random.Range(-yDifference, yDifference);
    42.         float xPos = Random.Range(-xDifference, xDifference);
    43.         Vector3 randomPos = new Vector3(xPos, yPos, 0);
    44.         return randomPos;
    45.  
    46.     }
    47. }
     
  2. Junglerally

    Junglerally

    Joined:
    May 4, 2020
    Posts:
    14
    My idea to do this is to have a starting platform, and every time an object is instantiated, I find the object with the tag "Platform" that is furthest away from the starting platform. I just don't know how I could script that (I am very new).
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    GenerateRandomPos() returns Vector3 so get it from there.
    or
    "Instantiate" returns an object so you can get it's transform and obtain position from there.

    Also see some pure C# tutorials before jumping to Unity with someone else code.
     
  4. Junglerally

    Junglerally

    Joined:
    May 4, 2020
    Posts:
    14
    This is my script, I wrote it myself. But about your answer, I don't really understand how I would get the same position for both the instantiate and the lastPos to add it when GenerateRandomPos() is called again. Sorry if my question is kind of confusing.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Instantiate() returns an object it created.

    Code (csharp):
    1.  
    2. var newObject = Instantiate(blahblahblah).
    3. var newPosition= newObject.transform.position;
    4.  
    Like this.
     
    Junglerally likes this.
  6. Junglerally

    Junglerally

    Joined:
    May 4, 2020
    Posts:
    14
    OMG THANK U SO MUCH! This worked perfectly! I really appreciate your help. I can't believe I didn't think of this. Thank you a lot!
     
    neginfinity likes this.
  7. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    reference manual is your best friend :D In this case even intellisense will do :p