Variables are getting permanently stored in prefabs

Hello!

I have a problem with prefabs. Before describing my actual problem here is the code that is causing my problem:


RessourceScript.js

#pragma strict

var gold : int = 0;
var wood : int = 0;
var stone : int = 0;
var clay : int = 0;
var iron : int = 0;
var sulphur : int = 0;
var silver : int = 0;
var bronze : int = 0;


function Costs(goldCost : int, woodCost : int, stoneCost : int, clayCost : int, ironCost : int, sulphurCost : int, silverCost : int, bronzeCost : int) {
	if (
		gold >= goldCost && 
		wood >= woodCost && 
		stone >= stoneCost && 
		clay >= clayCost && 
		iron >= ironCost && 
		sulphur >= sulphurCost && 
		silver >= silverCost && 
		bronze >= bronzeCost
		) 
	{
		gold -= goldCost;
		wood -= woodCost;
		stone -= stoneCost;
		clay -= clayCost;
		iron -= ironCost;
		sulphur -= sulphurCost;
		silver -= silverCost;
		bronze -= bronzeCost;
		return true;
	} else {
		return false;
	}
}

BuildingCosts.js

#pragma strict

var goldCost : int = 0;
var woodCost : int = 0;
var stoneCost : int = 0;
var clayCost : int = 0;
var ironCost : int = 0;
var sulphurCost : int = 0;
var silverCost : int = 0;
var bronzeCost : int = 0;
var textures : Texture[];
var cameraObject : Camera;
@HideInInspector
var ressource : RessourceScript;

function Build(position : Vector3) {
	ressource = cameraObject.GetComponent(RessourceScript);
	if (ressource.Costs(goldCost,woodCost,stoneCost,clayCost,ironCost,sulphurCost,silverCost,bronzeCost))
	{
	    var last = Instantiate (gameObject, Vector3(Mathf.Round(position.x),0,Mathf.Round(position.z)), Quaternion.identity);
	    var tex = Random.Range(0, textures.length);
	    last.renderer.material.SetTexture("_MainTex", textures[tex]);	    
	} else {
		Debug.Log("Failed");
	}
}

The BuildingCosts script is attached to every building prefab. (Costs for every building set in the inspector)
The RessourceScript is attached to the MainCamera.

My main problem right now is, that as soon as I call buildingScript.Build(Vector3 from a raycast here); it changes the gold variable in the Camera prefab, not the camera instance in the scene.

This one minute long YouTube video briefly shows my problem: http://www.youtube.com/watch?v=offaB2d0yvg

(Watch the game windows and the inspector).
I think that this is happening because the MainCamera is a prefab, but I havent known that a script can “edit” a prefab.
How can I work around this?

Thank you!

Rather than trying to set the camera in the inspector for every prefab, ensure that your camera object/prefab has the tag “MainCamera” and then just use

resource = camera.main.GetComponent(ResourceScript);

That simply means your “cameraObject” variable doesn’t reference the instance in the scene but the camera prefab. That’s the only possible reason :wink: