Understanding a Wire Script

Hey guys, I’ve got another problem I want to discuss.

Basically, I have a grid where objects can be placed. On one end there is a Generator. On the other end, there is an Outlet. You walk around the room and you can place wires on the floor which connect. If the wire is connected to the Generator, I want it to turn on. When the wires create a line that touches the Outlet the Generator, I want the Outlet to activate.

I’ve figured this out… sort of. There are some minor issues right now.

The wires spread the power through collision with each other and can only be activated when touching another piece of wire that is on.

I just basically made the generator generate a constantly changing variable, so that if the wire ever got disconnected, it wouldn’t update to the next change and would turn off.

It works for the most part, but every 3 seconds when the generator variable changes, there’s a split second when the wires turn off then quickly turn back on. I’m looking for a way to keep it either consistently on, or consistently off.

Also, the wire doesn’t actually stay on unless there are 2 instances of a wire in the same space next to the generator. I have no idea why, but if there is, only one the wire turns on and then off after 3 seconds.

Wire script (Called Power):

#pragma strict

var power : int = 0;

function Update() 
{
        // obtain generator variable
	var go = GameObject.Find("Generator");
	var genscript : Generator;
	genscript = go.GetComponent(Generator);

        // if the power is on
	if(power == genscript.genlevel)
	{
		renderer.material.color = Color(0, 255, 0);
	}
	else 
	{
		renderer.material.color = Color(192, 192, 192);
	}
}

function OnCollisionStay(col : Collision) 
{
       //obtain generator variable
	var go = GameObject.Find("Generator");
	var genscript : Generator;
	genscript = go.GetComponent(Generator);
        
        //if power is on
	if(power == genscript.genlevel)
	{
                //if touching another wire
		if(col.collider.tag == "Wire")
		{
                        //obtain other wire power level
			var otherscript : Power;

			otherscript = col.collider.GetComponent(Power);

                        //if the other wire's power level does not equal the generator variable
			if(otherscript.power != genscript.genlevel)
			{
                                //set the other wire variable to the generator variable
				otherscript.power = genscript.genlevel;
			}
		}
		else 
		{
                        //if touching an outlet
			if(col.collider.tag == "Outlet")
			{
                                //obtain outlet power level
				var outletscript : Outlet;
				outletscript = col.collider.GetComponent(Outlet);
			        
                                //if outlet power is not equal to generator variable
				if(outletscript.power != genscript.genlevel)
				{
                                        //set outlet power to generator level
					outletscript.power = genscript.genlevel;
				}
			}
			else
			{
                                //if touching a generator
				if(col.collider.tag == "Generator")
				{
                                        set power to generator level
					power = genscript.genlevel;
				}
			}
		}
	}
}

Generator Script:

#pragma strict
var genlevel : int;

function Start () 
{
	genlevel = 1;
	while(true)
	{
                //increase generator variable by 1 every 3 seconds
		genlevel++;
		yield WaitForSeconds(3);
	}
}


function OnCollisionStay(col : Collision) 
{
        //if touching a wire
	if(col.collider.tag == "Wire") 
	{
                //obtain wire power level
		var otherscript : Power;
		otherscript = col.collider.GetComponent(Power);
                //set power level to generator variable
		otherscript.power = genlevel;
	}
}

Outlet Script:

#pragma strict

var power : int = 0; 

function Update() 
{
       //obtain generator variable
	var go = GameObject.Find("Generator");
	var genscript : Generator;
	genscript = go.GetComponent(Generator);

        //if power is on
	if(power == genscript.genlevel)
	{
		renderer.material.color = Color(0, 255, 255);
	}
	else 
	{
		renderer.material.color = Color(0, 0, 255);
	}
}

So, for a recap: I’m looking for a way to change the code so that the wire’s don’t turn off for a split second whenever the generator variable is updated.

I’m also looking for a reason why the wire requires two separate instances of the wire inside each other next to the generator to make the rest of the wire work.

I can understand if this is difficult to understand through text, so if asked, I could provide the project.

Bump. I made progress, and provided much clearer and cleaner scripts. The problems I’m having now are smaller too. I’d appreciate anyone’s insight!