C# Electricity sim, blocks(wires) still power after severed

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.

22899-wiring.jpg

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

It looks like the problem would be here:
if(distance >= 2){
if(powerValue == 1){
powerValue = 0;
}

In your CalcDist Function. If you drop a block out of the middle, the new distance would be 2, so the first if passes, but nothing happens if the distance isn’t 1. So since it’s 2, it just stays powered. With how you have it set up, the if(powerValue ==1) will never happen since the one before it checks to make sure it is 2 or greater.

The check before this whole block also checks to make sure it equals 1, which can never be >= 2, so there’s that as well.

Your code is wrong in the foreach. If any active block is adjacent, the block will turn on, then if any block is farther away, it will turn off. Whether it turns on or off depends on the last block checked, which in a foreach, is totally unpredictable.

What you need to do is every time a block is removed, turn off all the blocks, then start at the generator and turn on all the blocks touching it, then turn on all the blocks touching those, etc.

Your problem is that you check if the distance is equal to one but you could have a distance of .98

Change this

    if(distance == 1){
          if(powerValue == 0){
              if(gameObj.name == "Generator" || gameObj.name == "Powered Block"){
                 powerValue = 1;
              }
          }
          if(distance >= 2){
              if(powerValue == 1){
                 powerValue = 0;
              }
          }
         }

To

 if(distance >= 2)
 {
     .. check power and stuff for turning off
 }
 else
 {
      ... check power and stuff for turning on
 }