Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Goodgamejuli · Aug 04, 2020 at 10:22 AM · randomcolorobjectsspawningcolor change

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;
                 }
 
             }
         }
 
 
 
 
 
 
 
 
 
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
      
         }
 }



Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Goodgamejuli · Aug 04, 2020 at 09:18 PM 0
Share

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! :)

avatar image Goodgamejuli · Aug 05, 2020 at 10:10 AM 0
Share

alt text

however, i still have the problem that sometimes an object like in the picture matches one of the template objects in shape and color.

tet.png (267.6 kB)
avatar image davidcox70 Goodgamejuli · Aug 05, 2020 at 10:27 AM 0
Share

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.

avatar image Goodgamejuli · Aug 05, 2020 at 11:31 AM 0
Share

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.alt text

unbenhannt.png (265.4 kB)
avatar image davidcox70 Goodgamejuli · Aug 05, 2020 at 11:43 AM 0
Share

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.

avatar image Goodgamejuli · Aug 05, 2020 at 01:04 PM 0
Share

Should I upload the project so that you can have a look at it?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random object spawn? 1 Answer

Make sure that game objects aren't the same color 2 Answers

Why are the UI Textures darkening when Built? 1 Answer

random object creation 1 Answer

How can i destroy cubes that is randomly generated when i have passed them? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges