Reputation: 1
So my question related to C# scripting in Unity, I want my block to change color each time it got hit (first time a block is hit it goes red, the second time it turns yellow, the third time it turns green and then at the fourth time the block should be destroyed.)I also tried to make an array like Color[]mycolor but doesn't seem to work so please send help if you can and thank you for reading. Down here are my codes. This is my BlockMaker script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockMaker : MonoBehaviour
{
public GameObject block;
private int blockWidth = 2;
private int blockHeight = 1;
List<GameObject> blockList = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
StartCoroutine(BuildWall());
}
// Update is called once per frame
void Update()
{
}
IEnumerator BuildWall()
{
for (int xAxis = 0; xAxis < 5; xAxis++)
{
for (int yAxis = 0; yAxis<5; yAxis++)
{
GameObject latestBlock = Instantiate(block, new Vector3(this.transform.position.x + blockWidth * xAxis, this.transform.position.y + blockHeight * yAxis, this.transform.position.z), Quaternion.identity);
blockList.Add(latestBlock);
Debug.Log("New block added at" +latestBlock.transform.position.x+"x :" +latestBlock.transform.position.y+"y");
yield return new WaitForSeconds(0.2f);
}
}
// it makes more sense to loop through blockList.Count, so that it will Destroy everything, not just the first 5 elements.
for (int xAxis = 0; xAxis < blockList.Count; xAxis++)
{
//Destroy(blockList[xAxis], 0.2f);
}
// this will clear out the list after we Destroy all the blocks
blockList.Clear();
}
}
And here is my BlockBehaviour script which is going to be used to manipulate the colors or something like that:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockBehaviour : MonoBehaviour
{
public Color mycolor;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter (Collision ObjectCollidedWith)
{
if(ObjectCollidedWith.collider.tag == "ammo")
{
Debug.Log("block detecting collision by " + ObjectCollidedWith.collider.tag);
gameObject.GetComponent<Renderer>().material.color = mycolor;
gameObject.tag = "redBlock";
Debug.Log("Tag changed to " + gameObject.tag);
}
if(gameObject.tag == "redBlock")
{
Destroy(gameObject);
}
else
{
gameObject.GetComponent<Renderer>().material.color = mycolor;
}
}
}
Upvotes: 0
Views: 654
Reputation: 105
I think you code should work just fine. First thing I want to know is whether you have the Collision or not. You can try to add a debug outside of the if check. Just a note, in most case, when you have issue with Collision, you may want to make sure there is at least on Rigidbody attached to one of the GO to make the Unity physics system work. Here out here: https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
void OnCollisionEnter (Collision ObjectCollidedWith)
{
if(ObjectCollidedWith.collider.tag == "ammo")
{
Debug.Log("block detecting collision by " + ObjectCollidedWith.collider.tag);
gameObject.GetComponent<Renderer>().material.color = mycolor;
gameObject.tag = "redBlock";
Debug.Log("Tag changed to " + gameObject.tag);
if(gameObject.tag == "redBlock")
{
Destroy(gameObject);
}
}
}
The collision detection code could be simplified with about code, move the if condition check into the collision check, I assume the color change only happens when the "ammo" hit the wall. If you have plan to have other mechanic other than using collision detection detection to handle this, then place the code in Update().
Another thing is changing the tag for this purpose is not ideal I think. How about have a lift property
int Life=4;
to help you better manage the the gameobject.
Upvotes: 1