- Home /
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
Show us your actual code so we can see what's wrong.
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];
 }
}
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;
     }
 }
something like this, when the 3 objects are same, the wall will be destroyed
[1]: /storage/temp/119023-uty.png
And on what occasion does this need to trigger because I see you're using OnCollision?
im using oncollisionenter and when the 3 objects are all red, blue or green, the wall will destroy
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;
         }
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                