- Home /
How do I randomly create 2 objects, each with a different shape and color?
In addition, the objects created must not match any object in color and shape as shown in the video. Please watch 1 minute from the following video to understand what i want to do: https://youtu.be/D2PJ7kWAs4Y?t=279
I can't get any further, maybe someone can help me, here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AusschlussObjekte : MonoBehaviour
{
public GameObject Kreis;
public GameObject Kreuz;
public GameObject Quadrat;
public GameObject Dreieck;
public GameObject Quader;
public GameObject Kreis2;
public GameObject Kreuz2;
public GameObject Quadrat2;
public GameObject Dreieck2;
public GameObject Quader2;
public int Formgenerator1;
public int Farbgenerator1;
public int Formgenerator2;
public int Farbgenerator2;
public int gespawnt = 0;
public int gespawnt2 = 0;
// Start is called before the first frame update
void Start()
{
}
void DestroyAll(string Finish)
{
GameObject[] Objects = GameObject.FindGameObjectsWithTag("Finish");
for (int i = 0; i < Objects.Length; i++)
{
Destroy(Objects[i]);
}
}
void Farbgenarator()
{
DestroyAll("Finish");
Formgenerator1 = Random.Range(1, 5);
Formgenerator2 = Random.Range(1, 5);
Farbgenerator1 = Random.Range(1, 5);
Farbgenerator2 = Random.Range(1, 5);
Generatorcheck();
}
void Generatorcheck()
{
if ((Formgenerator1 != Formgenerator2) & (Formgenerator1 == 1 && Farbgenerator1 != 5 || Formgenerator1 == 2 && Farbgenerator1 != 4 || Formgenerator1 == 3 && Farbgenerator1 != 3 || Formgenerator1 == 4 && Farbgenerator1 != 2 || Formgenerator1 == 5 && Farbgenerator1 != 1) & (Formgenerator2 == 1 && Farbgenerator2 != 5 || Formgenerator2 == 2 && Farbgenerator2 != 4 || Formgenerator2 == 3 && Farbgenerator2 != 3 || Formgenerator2 == 4 && Farbgenerator2 != 2 || Formgenerator2 == 5 && Farbgenerator2 != 1))
{
gespawnt = 1;
gespawnt2 = 1;
}
else
{
gespawnt = 0;
gespawnt2 = 0;
Farbgenarator();
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space"))
{
Farbgenarator();
}
if (gespawnt == 1)
{
if (Formgenerator1 == 1)
{
Instantiate(Kreis, new Vector3(0, 1, 1), Quaternion.identity);
if (Farbgenerator1 == 1)
{
GameObject.Find("Kreis(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator1 == 2)
{
GameObject.Find("Kreis(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator1 == 3)
{
GameObject.Find("Kreis(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator1 == 4)
{
GameObject.Find("Kreis(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
gespawnt = 2;
}
if (Formgenerator1 == 2)
{
Instantiate(Kreuz, new Vector3(0, 1, 1), Quaternion.identity);
if (Farbgenerator1 == 1)
{
GameObject.Find("Kreuz(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator1 == 2)
{
GameObject.Find("Kreuz(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator1 == 3)
{
GameObject.Find("Kreuz(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator1 == 5)
{
GameObject.Find("Kreuz(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
gespawnt = 2;
}
if (Formgenerator1 == 3)
{
{
Instantiate(Quadrat, new Vector3(0, 1, 1), Quaternion.identity);
if (Farbgenerator1 == 1)
{
GameObject.Find("Quadrat(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator1 == 2)
{
GameObject.Find("Quadrat(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator1 == 4)
{
GameObject.Find("Quadrat(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
if (Farbgenerator1 == 5)
{
GameObject.Find("Quadrat(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
gespawnt = 2;
}
}
if (Formgenerator1 == 4)
{
{
Instantiate(Dreieck, new Vector3(0, 1, 1), Quaternion.identity);
if (Farbgenerator1 == 1)
{
GameObject.Find("Dreieck(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator1 == 3)
{
GameObject.Find("Dreieck(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator1 == 4)
{
GameObject.Find("Dreieck(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
if (Farbgenerator1 == 5)
{
GameObject.Find("Dreieck(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
gespawnt = 2;
}
}
if (Formgenerator1 == 5)
{
{
Instantiate(Quader, new Vector3(0, 1, 1), Quaternion.identity);
if (Farbgenerator1 == 2)
{
GameObject.Find("Quader(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator1 == 3)
{
GameObject.Find("Quader(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator1 == 4)
{
GameObject.Find("Quader(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
if (Farbgenerator1 == 5)
{
GameObject.Find("Quader(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
gespawnt = 2;
}
}
}
///////////////////////////////////////////////////////////////
if (gespawnt2 == 1)
{
if (Formgenerator2 == 1)
{
{
Instantiate(Kreis2, new Vector3(0, 1, -1), Quaternion.identity);
if (Farbgenerator2 == 1)
{
GameObject.Find("Kreis2(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator2 == 2)
{
GameObject.Find("Kreis2(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator2 == 3)
{
GameObject.Find("Kreis2(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator2 == 4)
{
GameObject.Find("Kreis2(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
gespawnt2 = 2;
}
}
if (Formgenerator2 == 2)
{
{
Instantiate(Kreuz2, new Vector3(0, 1, -1), Quaternion.identity);
if (Farbgenerator2 == 1)
{
GameObject.Find("Kreuz2(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator2 == 2)
{
GameObject.Find("Kreuz2(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator2 == 3)
{
GameObject.Find("Kreuz2(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator2 == 5)
{
GameObject.Find("Kreuz2(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
gespawnt2 = 2;
}
}
if (Formgenerator2 == 3)
{
{
Instantiate(Quadrat2, new Vector3(0, 1, -1), Quaternion.identity);
if (Farbgenerator2 == 1)
{
GameObject.Find("Quadrat2(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator2 == 2)
{
GameObject.Find("Quadrat2(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator2 == 4)
{
GameObject.Find("Quadrat2(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
if (Farbgenerator2 == 5)
{
GameObject.Find("Quadrat2(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
gespawnt2 = 2;
}
}
if (Formgenerator2 == 4)
{
{
Instantiate(Dreieck2, new Vector3(0, 1, -1), Quaternion.identity);
if (Farbgenerator2 == 1)
{
GameObject.Find("Dreieck2(Clone)").GetComponent<Renderer>().material.color = Color.red;
}
if (Farbgenerator2 == 3)
{
GameObject.Find("Dreieck2(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator2 == 4)
{
GameObject.Find("Dreieck2(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
if (Farbgenerator2 == 5)
{
GameObject.Find("Dreieck2(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
gespawnt2 = 2;
}
}
if (Formgenerator2 == 5)
{
{
Instantiate(Quader2, new Vector3(0, 1, -1), Quaternion.identity);
if (Farbgenerator2 == 2)
{
GameObject.Find("Quader2(Clone)").GetComponent<Renderer>().material.color = Color.green;
}
if (Farbgenerator2 == 3)
{
GameObject.Find("Quader2(Clone)").GetComponent<Renderer>().material.color = new Color(1.0f, 0.64f, 0.0f);
}
if (Farbgenerator2 == 4)
{
GameObject.Find("Quader2(Clone)").GetComponent<Renderer>().material.color = Color.yellow;
}
if (Farbgenerator2 == 5)
{
GameObject.Find("Quader2(Clone)").GetComponent<Renderer>().material.color = Color.blue;
}
gespawnt2 = 2;
}
}
}
}
}
Answer by davidcox70 · Aug 04, 2020 at 11:55 AM
This solution uses two concepts. Firstly, it makes a "List" of possible objects and a list of possible colors. This means that each shape has an index number, from 0 up to the number of shapes minus 1. Same for the colors. To make the first object, we simply pick a random number for the object and color. For the second object, we add to the index of the first object. So long as we add more than 0, we are guaranteed to end up on a different number. But this would go out of range (the index number would be higher than the number of shapes), so we loop the result around with a % operator. So long as we add a number between 1 and the number of shapes-1, we will always end up with a different shape. We do the same for picking the color of the second object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shapeGenerator : MonoBehaviour
{
// drag the relevant model into these slots in the editor inspector
public GameObject starShape;
public GameObject triangleShape;
public GameObject squareShape;
public GameObject circleShape;
public GameObject flatShape;
// we'll use these lists to hold the shapes and colors.
private List<Color> colors;
private List<GameObject> shapes;
// we'll use these to hold the randomly created objects
private GameObject object1;
private GameObject object2;
// create two materials and drag them into these slots in the editor.
public Material materialForObject1;
public Material materialForObject2;
private void Start()
{
// add all the shapes into a single list;
shapes = new List<GameObject>();
shapes.Add(starShape);
shapes.Add(triangleShape);
shapes.Add(squareShape);
shapes.Add(circleShape);
shapes.Add(flatShape);
// hide all the source objects by going through the list and setting them to inactive.
foreach(GameObject go in shapes)
{
go.SetActive(false);
}
// make a list of possible colors;
colors = new List<Color>();
colors.Add(new Color(1, 0, 0)); // red
colors.Add(new Color(0, 1, 0)); // green
colors.Add(new Color(0, 0, 1)); // blue
colors.Add(new Color(1, 1, 0)); // yellow
colors.Add(new Color(1, 0, 1)); // purple
// choose our two objects
selectObjects();
}
private void selectObjects()
{
// to make object 1, we simply need to select any object and any colour from our lists.
// pick a random numbered shape and color from the list.
int object1Index = Random.Range(0, shapes.Count);
int object1Color= Random.Range(0, colors.Count);
// set object1 to be the selected shape
object1 = shapes[object1Index];
// set the material for object1's color
materialForObject1.color = colors[object1Color];
// set the material for object 1
object1.GetComponent<Renderer>().material = materialForObject1;
// move it so it's not on top of object 2
object1.transform.position = new Vector3(-3, 0, 0);
// make the object visible (we made it invisible in Start());
object1.SetActive(true);
// object1 done - now object 2, ensuring it is neither the same color or shape as object 1.
// if we take the index of object 1's shape and add an offset to it, it cannot end up on the same number.
int object2Index = object1Index + Random.Range(1, shapes.Count - 1);
// ... but it would go out of range (a higher index than the number of shapes) so we loop it around with the % operator.
// as long as the offset we added to the first index is between 1 and the number of shapes-1, we are guaranteed to end up on a different number.
object2Index = object2Index % shapes.Count;
// do the same for color.
int object2Color = object1Color + Random.Range(1, colors.Count - 1);
object2Color = object2Color % colors.Count;
// set object2 to be the selected shape
object2 = shapes[object2Index];
// set the material for object2's color
materialForObject2.color = colors[object2Color];
// set the material for object 2
object2.GetComponent<Renderer>().material = materialForObject2;
// move it so it's not on top of object 1
object2.transform.position = new Vector3(3, 0, 0);
// make the object visible (we made it invisible in Start());
object2.SetActive(true);
}
}
Thank you for the great effort! I was already desperate, but your concept works great. I'm still relatively new to UNITY, but I've learned a lot today. Thank you! :)
however, i still have the problem that sometimes an object like in the picture matches one of the template objects in shape and color.
hmmm. I'm not sure how your game runs, but do you generate the shapes multiple times during the game or only once at the start? If you generate them multiple times, it could be that the old selection isn't being cleared. So the code is generating new objects, but they are underneath an older object. To ensure this isn't the case (assu$$anonymous$$g new selections are made during the game), then run the lines of code between 40 and 45 (forEach...) before you generate new options. This will hide the old ones.
Have to check that again .. I have successfully managed to compare the lists in terms of shape and color, but I still found a problem that in the end 2 options remain open.
Interesting! How often are you seeing the duplicate? I just ran the code 20,000 times without a coincidence. Just double check that the same shape isn't added twice in the editor slots, or that the same color isn't added twice to the list. A good test to do is that object1Index should never be the same as object2Index. Also that object1Color never equals object2Color. If they ever do agree, then there is a code issue. If they never agree, then the duplicates are co$$anonymous$$g from some error relating to the contents of the list.
Should I upload the project so that you can have a look at it?