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

Randomize gameobjects

Discussion in 'Scripting' started by chemsoun, May 20, 2015.

  1. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    hi everyone

    i have multiple gameobjects in my scene (24 gameobjects), i made it in randomly showing the half of them. but the issue is that they appear outside my scene.the strange thing is that the position values of one gameobject is exactly the same of the original gameobject but it's not in the same position

    i'm using this code that i attched to an empty gameobject

    Code (CSharp):
    1. public GameObject[] aliments
    2.         ;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.    
    7.  
    8.         for (int i =0; i<aliments.Length/2; i++) {
    9.             int randomInt = Random.Range (0, aliments.Length - 1);
    10.        
    11.             Instantiate (aliments [randomInt]);
    12.  
    13.  
    14.         }
    15.     }
    16.    
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Are your "aliments" object prefabs, or are they objects in the scene?
     
  3. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    objects in the scene. should i haven't use Instantiate ?
     
  4. Deleted User

    Deleted User

    Guest

    Instantiate creates a new copy of a GameObject.
     
  5. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    i noticed that . is there any solution that doesn't have to create a copy of my gameobject and randomly shows a number them not all of them
     
  6. Deleted User

    Deleted User

    Guest

    The best thing might be to enable / disable them as needed. You can use gameObject.SetActive(false) to disable / enable them.
     
  7. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    i don't see how enabling or disabling them helps to randomly showing some of them
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Code (csharp):
    1. for (int i=0;i< aliments.Length;i++) {
    2. ailments[i].SetActive(Random.value > 0.5f);
    3. }
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

    When a GO is disabled it won't be rendered.
     
  10. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    If they are disabled they wont be visible or interact with anything in your scene. Just start them out disabled and then enable half of them randomly.
     
  11. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    it works like a charm, thanks :)
     
  12. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    thank you all for your answers, i've tried the code of StarManta and it works