- Home /
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!
Answer by Durayel · Nov 24, 2013 at 03:56 PM
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);
Answer by Bunny83 · Nov 24, 2013 at 03:16 PM
That simply means your "cameraObject" variable doesn't reference the instance in the scene but the camera prefab. That's the only possible reason ;)
The problem is that I cant drag the Camera instance in the scene onto the cameraObject variable. It only allows me to use prefabs for that.
To me it looks like you're executing all your code on the prefabs and not on instances. A prefab can never reference an object in the scene.
Where do you call the Build method and on what object?
The Build method is being called from the BuildingScript, which is also attached to the $$anonymous$$ainCamera prefab:
#pragma strict
var buildings : GameObject[]; // An Array with all building GameObjects
@HideInInspector
var activeBuilding : int = 0; // The Building were going to build
function Start () {
Debug.Log(buildings[0]);Debug.Log(buildings.length);
}
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)){
var ray = camera.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){
Debug.Log(buildings[activeBuilding]);
var buildingScript = buildings[activeBuilding].GetComponent(BuildingCosts);
buildingScript.Build(hit.point); // <-- !! !! !! !!
}
}
}
function OnGUI () {
if (GUI.Button (Rect (Screen.width-130,Screen.height-50,120,40), "Building")) {
if (activeBuilding < buildings.Length-1)
{
activeBuilding++;
Debug.Log(activeBuilding);
}
else
{
activeBuilding=0;
Debug.Log(activeBuilding);
}
}
GUI.Label(Rect (10,Screen.height-50,200,40), ""+buildings[activeBuilding]);
}
"To me it looks like you're executing all your code on the prefabs and not on instances. A prefab can never reference an object in the scene."
But I dont have the GameObjects I want to Instantiate in the scene. The script instantiates prefabs(Like a Tower), but these prefabs need references to objects in the scene(Like my $$anonymous$$ainCamera, which holds the information about the ressources).