Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 navjothb · Jun 16, 2018 at 10:35 AM · destroycolorcolor changedestroy objectcolors

i'm trying to code a premise, if 3 objects are the same color then it will destroy the wall

i'm trying to make the code for the destroy, like if red && red &&red then destroy game object

Comment
Add comment · Show 2
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 ShadyProductions · Jun 16, 2018 at 10:47 AM 0
Share

Show us your actual code so we can see what's wrong.

avatar image navjothb · Jun 16, 2018 at 11:20 AM 0
Share

i just currently have this i havent yet figured out how to do it public class colors : $$anonymous$$onoBehaviour {

 void OnCollisionEnter(Collision col)
 {
     Color[] c = new Color[3] { Color.red, Color.blue, Color.green };
     int random = Random.Range(0, 3);
     this.gameObject.GetComponent<Collider>().GetComponent<$$anonymous$$eshRenderer>().material.color = c[random];

 }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ShadyProductions · Jun 16, 2018 at 12:08 PM

Perhaps something like this?:

 public class Colors : MonoBehaviour
 {
     private Renderer _renderer;
     private Color[] _colors;
 
     private void Start()
     {
         // Get renderer of this monobehaviour only once at start
         _renderer = GetComponent<Renderer>();
 
         // Setup the _colors array only once at start
         _colors = new [] { Color.red, Color.blue, Color.green};
     }
 
     private void OnCollisionEnter(Collision col)
     {
         // Get a random color from the _colors array
         Color color = _colors[Random.Range(0, _colors.Length)];
 
         // Do something if color matches criteria
         if (color == Color.red)
         {
 
         }
         else if (color == Color.blue)
         {
 
         }
         else if (color == Color.green)
         {
 
         }
 
         // Set the color on the material
         _renderer.material.color = color;
     }
 }
Comment
Add comment · Show 9 · 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 navjothb · Jun 16, 2018 at 01:27 PM 0
Share

something like this, when the 3 objects are same, the wall will be destroyed

[1]: /storage/temp/119023-uty.png

uty.png (101.6 kB)
avatar image ShadyProductions navjothb · Jun 16, 2018 at 01:31 PM 0
Share

And on what occasion does this need to trigger because I see you're using OnCollision?

avatar image navjothb ShadyProductions · Jun 16, 2018 at 01:38 PM 0
Share

im using oncollisionenter and when the 3 objects are all red, blue or green, the wall will destroy

avatar image ShadyProductions navjothb · Jun 16, 2018 at 01:39 PM 0
Share

Perhaps this then.. You can assign the objects in the inspector. Put this script on 1 gameobject, like a gamemanager empty gameobject or something.

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 public class WallDestroyer : $$anonymous$$onoBehaviour
 {
     public GameObject[] Cubes;
     public GameObject Wall;
     private List<Renderer> _rendererCache;
 
     private void Start()
     {
         // Fill up renderer cache
         _rendererCache = new List<Renderer>();
         foreach (var cube in Cubes)
             _rendererCache.Add(cube.GetComponent<Renderer>());
     }
 
     private void Update()
     {
         if (_rendererCache.All(f => f.material.color == Color.red || f.material.color == Color.green || f.material.color == Color.blue) && Wall != null)
         {
             Destroy(Wall);
             Wall = null;
         }
     }
 }
avatar image navjothb ShadyProductions · Jun 16, 2018 at 01:43 PM 0
Share

thank you very much

Show more comments
Show more comments

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

90 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

Related Questions

User should be able to pick a Gradient color and apply them linear or circular from the touch point taken as the origin. 1 Answer

Convert RGB to RYB color scheme? 0 Answers

how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers

Color of some instantiated objects differs from the normal ones. 1 Answer

My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers


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