Home > Software design >  Display only one gameobject from list
Display only one gameobject from list

Time:11-25

I have 3(A) game object I want when game start to display random only one another to hide,

enter image description here

I tried below code but is only change position please help

    public GameObject player;
   public float placex;
   public float placez;
   private void Start() {
    placex=Random.Range(209,-70);
    placez=Random.Range(-179,108);

    player.transform.position=new Vector3(placex,0,placez);
   }

CodePudding user response:

Alright so first: setting the transform-position does exactly what it says: it sets the position of the transform i.E. the object-position. What you want is more likely to activate/disable the objects.

[SerializeField]
private List<GameObject> objects = new List<GameObject>();

private void Start()
{
  int randomlyActiveObject = UnityEngine.Random.Range(0, objects.Count);
  for (int i = 0; i < objects.Count; i  )
  {
    objects[i].SetActive(i == randomlyActiveObject);
  }
}

So what this does is giving you the option to select multiple objects in a list and at Start, selects one randomly to activate while all the others become disabled. You probably want to place this on a seperate object though.

Nevertheless looking at you code I can't help but think, that you copied that code from somewhere without really thinking or understanding what it does since it has absolutelly nothing to do with your problem at all. And it's not difficult to see that it has nothing to do with what you're asking. It's no shame starting off by copying other peoples code to see what it does but you'll never get better at this if you don't truly understand what that code does.

And most people here on Stackoverflow are gonna roll their eyes with this kind of stuff since it just screams "I'm a beginner and i need help" and sorry but this is page is for problem-solving, not tutorials. Get a better understanding of Unity and C# and if you have a genuine question that you haven't been able to figure out after hours of trying and researching feel freen to come back and ask for help. But not for something as trivial as "disable all but one object"

Anyway. Hope I could help. And sorry for the mild rant.

  • Related