0

In my 2D game I have randomized objects which are spawned as 4 to 5 clones each time I run the game. My problem is that I also have a different object that I want to spawn as 1 clone, and position it to appear after the last clone of the randomized objects I have in my game.

The objects randomization works perfectly in my game, I just need to separate that from the object that I want it to be spawned independently and after the last clone of the randomized objects.

This is the code I am using with 1 line of attempt to spawn the independent object: (The code was taken from this tutorial)

 using UnityEngine;
    using System;
    using System.Collections.Generic;       //Allows us to use Lists.
    using Random = UnityEngine.Random;      //Tells Random to use the Unity Engine random number generator.

    namespace Completed

    {

        public class BoardManager : MonoBehaviour
        {

            // Using Serializable allows us to embed a class with sub properties in the inspector.
            [Serializable]
            public class Count
            {
                public int minimum;             //Minimum value for our Count class.
                public int maximum;             //Maximum value for our Count class.


                //Assignment constructor.
                public Count (int min, int max)
                {
                    minimum = min;
                    maximum = max;
                }
            }


            public int columns = 7;                                         //Number of columns in our game board.
            public Count random1Count = new Count (1, 2);                       //Lower and upper limit for our random number of objects
            public Count random2Count = new Count (1, 1);
            public Count random3Count = new Count (1, 1);
            public Count random4Count = new Count (1, 1);

            public GameObject[] randomObject1;                                  //Array of objects prefabs.
            public GameObject[] randomObject2;
            public GameObject[] randomObject3;
            public GameObject[] randomObject4;

            public GameObject obj; // the independent object declaration

            private List <Vector3> gridPositions = new List <Vector3> ();   //A list of possible locations to place objects.


            //Clears our list gridPositions and prepares it to generate a new board.
            void InitialiseList ()
            {
                //Clear our list gridPositions.
                gridPositions.Clear ();

                //Loop through x axis (columns).
                for(int x = 2; x < columns; x++)
                {


                    //At each index add a new Vector3 to our list with the x and y coordinates of that position.
                    gridPositions.Add (new Vector3(x, 0.3f, 0f));

                    Instantiate(obj); // my attempt to instantiate the separate object
                    Debug.Log(obj.transform.position.x); // my attempt to track the position of the separate object
                }

            }


            //RandomPosition returns a random position from our list gridPositions.
            Vector3 RandomPosition ()
            {
                //Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
                int randomIndex = Random.Range (0, gridPositions.Count);

                //Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
                Vector3 randomPosition = gridPositions[randomIndex];


                //Remove the entry at randomIndex from the list so that it can't be re-used.
                gridPositions.RemoveAt (randomIndex);


                //Return the randomly selected Vector3 position.
                return randomPosition;

            }


            //LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
            void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
            {
                //Choose a random number of objects to instantiate within the minimum and maximum limits
                int objectCount = Random.Range (minimum, maximum+1);

                //Instantiate objects until the randomly chosen limit objectCount is reached
                for(int i = 0; i < objectCount; i++)
                {

                    //Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
                    Vector3 randomPosition = RandomPosition();




                    //Choose a random tile from tileArray and assign it to tileChoice
                    GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];


                    //Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
                    Instantiate(tileChoice, randomPosition, Quaternion.identity);

                }

            }


            //SetupScene initializes our level and calls the previous functions to lay out the game board
            public void SetupScene (int level)
            {

                //Reset our list of gridpositions.
                InitialiseList ();

                //Instantiate a random number of objects based on minimum and maximum, at randomized positions.
                LayoutObjectAtRandom (randomObject1, random1Count.minimum, random1Count.maximum);
                LayoutObjectAtRandom (randomObject2, random2Count.minimum, random2Count.maximum);
                LayoutObjectAtRandom (randomObject3, random3Count.minimum, random3Count.maximum);
                LayoutObjectAtRandom (randomObject4, random4Count.minimum, random4Count.maximum);


            }
        }
    }
Maryoomi1
  • 113
  • 1
  • 3
  • 16
  • 1
    I don't understand what it is you're trying to do. What do you mean by "position it to appear after the last clone"? Do you want it to spawn later (in time) or in a position that is in some way "after" (in a line for example)? – Adam H Apr 29 '15 at 13:10
  • So, are you looking for us to write code for you? SO is created for answering specific questions. – Max Yankov Apr 29 '15 at 13:39
  • Adam H: Yes, I mean that the object should be positioned in line after the last clone of randomized objects according to the x axis. – Maryoomi1 Apr 29 '15 at 17:29
  • Max Yankov: I have already made like 1000 attempts to make it work + plus I have already made adjustments in the attached code to what suits my project and the randomized objects worked perfectly with me. I also added a line in the code as an attempt to spawn the separate object. I would really appreciate your help! – Maryoomi1 Apr 29 '15 at 17:32
  • I apologize for the typo (I typed indecently instead of independently =_="). I have fixed the word now! sorry for the typing error again! – Maryoomi1 Apr 29 '15 at 17:44

1 Answers1

0

When you instantiate your final GameObject, you're not giving it a position. As I understand it you want to position it in the same place as your last randomly positioned GameObject, but offset in the x axis. To do this you'll need the Vector3 that contains the position of the last randomly spawned GameObject, I'll call this lastRandomPos. I assume you know how to get this value, if not leave a comment and I'll include it in my answer. Then you can pass the position when instantiating your final GameObject:

var objPos = lastRandomPos;
objPos.x = objPos.x + <however much you want to offset in x>;
Instantiate(obj, objPos, Quaternion.identity);

EDIT: The easiest way for you to get the position of the last random object is to just set a class-level variable every time you Instantiate an object in LayoutObjectAtRandom. Declare it in the class scope, for example underneath your declaration of gridPositions:

private List <Vector3> gridPositions = new List <Vector3> ();   //A list of possible locations to place objects.
private Vector3 lastRandomPos;

Then when you Instantiate a random object you can set this variable to keep track of where it was spawned:

Instantiate(tileChoice, randomPosition, Quaternion.identity);
lastRandomPos = randomPosition;
Adam H
  • 1,523
  • 11
  • 21
  • Thank you very much @AdamH. I really apologize I just saw the notification of your answer. I have previously tried the approach you provided and it did not work for me. I guess the reason of this is I am still not able to find the right way of getting the last random object's position. I have searched many solutions to help me find it, but as you know I am using a different way in my code to display my game objects. I really hope you find my comment and will appreciate it if you could help me with this. – Maryoomi1 Jun 10 '15 at 19:00
  • @Maryoomi1 I've edited my answer to explain how you can keep track of the position of the last random object. There are probably better ways but this is one of the simplest. – Adam H Jun 10 '15 at 22:43
  • Many thanks @AdamH for getting back to my question. I appreciate your help. I think the solution you provided will work if I only have 1 spawned object. Could you please give me a way of getting the position of the very last randomly spawned object ( in consideration that the total number of randomly spawned objects vary from 4 to 5 objects at each time the game is played)? – Maryoomi1 Jun 11 '15 at 11:46
  • Have you tried the solution in my edited answer? It should do exactly what you've asked for. `lastRandomPos` will always contain the position of the very last randomly spawned object (once the first one has been spawned) as its value will be overwritten whenever an object is randomly spawned. – Adam H Jun 11 '15 at 13:19
  • I do get one value using your solution, however when I double check if it's the right value I find out that it's not the right one. Thank you very much though! – Maryoomi1 Jun 11 '15 at 20:16