Hi, Im building a game loosely based on redstone wiring from Minecraft. You have a source block,(generator/battery/etc) and then you have the wire block that connects the sources to a power-able object.
The problem:
My generator block powers wire blocks that are connected to it. The wires, if powered, power any connected wires creating a trail that leads the power to the object you want to power. If you sever the connection of the wire at any point, by destroying a block, the blocks that are not connected to the generator still have power, the power state is saved in an int, if powered it is 1 if not, it is 0. but the int never updates. Does anyone have any advice on this, or maybe even a solution, something that i failed to notice?
Thanks in advanced.
Here is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ClampBlock : MonoBehaviour {
private int powerValue = 0;
private float distance;
// Use this for initialization
void Start () {
transform.position = new Vector3(Mathf.RoundToInt(transform.position.x), Mathf.RoundToInt(transform.position.y), Mathf.RoundToInt(transform.position.z));
}
void Update(){
CalcDist ();
if(powerValue == 1){
this.gameObject.name = "Powered Block";
this.renderer.material.color = Color.yellow;
}
if(powerValue == 0){
this.gameObject.name = "Non-Powered Block";
this.renderer.material.color = Color.blue;
}
}
void OnMouseEnter(){
this.renderer.material.color = Color.red;
}
void OnMouseExit(){
this.renderer.material.color = Color.white;
}
void OnMouseOver(){
if(Input.GetMouseButtonUp (1)){
Destroy(this.gameObject);
}
}
void CalcDist(){
foreach (var gameObj in GameObject.FindGameObjectsWithTag("GameObject") as GameObject[])
{
distance = Vector3.Distance(transform.position,gameObj.transform.position);
if(distance == 1){
if(powerValue == 0){
if(gameObj.name == "Generator" || gameObj.name == "Powered Block"){
powerValue = 1;
}
}
if(distance >= 2){
if(powerValue == 1){
powerValue = 0;
}
}
}
}
}
}