Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GameOjbect exists

Discussion in 'Scripting' started by Ivanvan12345, Apr 1, 2016.

  1. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    Hello,

    I have two scripts. A Spawn Script and a Player Script as shown below.

    Now in both scripts it will instantiate random prefabs from an array.

    Spawn script with an array size of: 16
    Player script with an array size of: 4

    Is there maybe a way to make sure that if a prefab is instantiated from the Player script, there will always be a match available.

    Look at my Egg.png image.
    The center egg is from the Player script and the eggs around is from the Spawn script.

    Example:
    So lets say the center egg is blue, there must always be a blue around somewhere.
    Lets say the center egg is purple, there must always be a purple around somewhere.

    Spawn Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour
    5. {
    6.     public GameObject[] milkies;
    7.  
    8.     void Start()
    9.     {
    10.         GameObject milky_01 = Instantiate(milkies[Random.Range (0,15)], new Vector3(0, 1.78f, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
    11.         milky_01.transform.parent = gameObject.transform;
    12.  
    13.         GameObject milky_02 =Instantiate(milkies[Random.Range (0,15)], new Vector3(1.9f, 1.06f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    14.         milky_02.transform.parent = gameObject.transform;
    15.  
    16.         GameObject milky_03 =Instantiate(milkies[Random.Range (0,15)], new Vector3(2.62f, -0.84f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    17.         milky_03.transform.parent = gameObject.transform;
    18.  
    19.         GameObject milky_04 =Instantiate(milkies[Random.Range (0,15)], new Vector3(1.9f, -2.74f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    20.         milky_04.transform.parent = gameObject.transform;
    21.  
    22.         GameObject milky_05 =Instantiate(milkies[Random.Range (0,15)], new Vector3(0, -3.46f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    23.         milky_05.transform.parent = gameObject.transform;
    24.  
    25.         GameObject milky_06 =Instantiate(milkies[Random.Range (0,15)], new Vector3(-1.9f, -2.74f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    26.         milky_06.transform.parent = gameObject.transform;
    27.  
    28.         GameObject milky_07 =Instantiate(milkies[Random.Range (0,15)], new Vector3(-2.62f, -0.84f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    29.         milky_07.transform.parent = gameObject.transform;
    30.  
    31.         GameObject milky_08 =Instantiate(milkies[Random.Range (0,15)], new Vector3(-1.9f, 1.06f, 0), Quaternion.Euler(0, 0, 0))as GameObject;
    32.         milky_08.transform.parent = gameObject.transform;
    33.     }
    34. }
    35.  
    Player
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public GameObject[] player;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         GameObject main_Holder = Instantiate(player[Random.Range (0,4)], new Vector3(0, -0.84f, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
    12.         main_Holder.transform.parent = gameObject.transform;
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.    
    19.     }
    20. }
    21.  
    This is my only problem I have in my game.
     

    Attached Files:

    • Eggs.png
      Eggs.png
      File size:
      144.9 KB
      Views:
      659
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Once you know the color of the center egg, instantiate an egg of the same color in a random location. Then fill up the remaining slots with randomly colored eggs.
     
  3. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    I don't know how to do that.

    It is a head scratcher for me.
    How do I need to modify my code?
     
  4. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    I think you might need to evaluate your design first maybe? If you don't know what the centre eggs colour will be before you instantiate it, then consider why that is.

    Regardless, my suggestion would be to maybe have an int to store the number in the array that you are spawning and a bool to check if you have spawned the second one of the same color.

    People will be a little reluctant to write code for you, purely because the real success is you having that eureka moment and solving it yourself ;)

    You obviously know how to code so yeah. Try deciding before you spawn anything what those first two spawns will be? Ofc thats just one idea how this could be done xD
     
  5. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    The player script is the one instantiating the middle egg right? Instead of directly using Random.Range in the call to Instantiate, saved the random value in an integer.

    Code (CSharp):
    1. int selectedEgg = Random.Range(0, 4);
    Pass selectedEgg as the player array index when instantiating the center egg. Your other script now needs to know what that value is. There are a number of ways to deal with that, but probably the simplest way given your current structure would be to make selectedEgg a public property on your player script and have your other script get the value and use it to instantiate another egg using the same value. Go to Unity Answers and do a search for accessing a variable from another script - it's one of the most commonly asked questions there.
     
    Ivanvan12345 likes this.
  6. Ivanvan12345

    Ivanvan12345

    Joined:
    Sep 12, 2014
    Posts:
    17
    Thank you so much.

    Now I know how to get the int of the instantiated egg.

    If I have any further inquiries, I will ask.
     
    Dave-Carlile and frekiidoochmanz like this.