- Home /
Strange error with increment-decrement between scripts
*bad engrish incoming"
Hi everyone
Basycally i'm creating a game where you have to drag into the scene some objects to do things.You also have an "energy bar". Everytime you spawn an object, the energy decreases. If it reaches zero, you can't spawn anymore object; however if you drag one of the dragged object into a "garbage icon" it increases again.
So i created a "generator" of the object to drag: the portion of the script that spawns the object is that:
public Transform clone; //a clone of block
public Transform block; //the block that i need to be renderized
void OnMouseDown()
{
print("touched!");
if (energy.GetComponent<Energyscript>().avaiable >= required)
{
clone = (Transform)Instantiate(block, transform.position, Quaternion.identity);
energy.GetComponent<Energyscript>().avaiable -= required; //required is the amount of energy required from the object to spawn
print(energy.GetComponent<Energyscript>().avaiable);
}
else //if the energy is not enough
{
//beep!
print("beep");
}
}
the energy script is basically that:
public class Energyscript: MonoBehaviour { public int avaiable; }
Easy. you set directly into the scene the amount of energy you want.
However that's the script inside the object to be spawned after touching the generator (without some parts that surely aren't the problem)
void OnMouseUp()
{
//if we dragged the object into the garbage icon
if (nullArea)
{
if (slot != null) //nevermind about slot. look immediately above
{
slot.GetComponent<Slot>().occupato = false;
}
//look at here and all of the similiar parts:
energy.GetComponent<Energyscript>().avaiable+= required;
print(energy.GetComponent<Energyscript>().avaiable);
Destroy(gameObject);
}
if (!play.gameObject.GetComponent<playControl>().toccato)
{
toccato = false;
slot = cercaVicino();
if (slot != null && !nullArea)
{
if (!slot.GetComponent<Slot>().occupato)
{
transform.position = new Vector3(slot.transform.position.x, slot.position.y, 0.0F);
slot.GetComponent<Slot>().occupato = true;
}
else
{
transform.position = latest;
slot = cercaVicino();
slot.GetComponent<Slot>().occupato = true;
}
}
}
}
void Update()
{
if (Input.GetMouseButtonDown(0) && creato)
{
toccato = true;
}
if(Input.GetMouseButtonUp(0) && creato)
{
slot = cercaVicino();
if (areaNulla)
{
if (slot != null)
{
slot.GetComponent<Slot>().occupato = false;
}
energy.GetComponent<Energyscript>().avaiable += required;
print(energy.GetComponent<Energyscript>().avaiable);
Destroy(gameObject);
}
else
{
if (slot != null)
{
if (slot.GetComponent<Slot>().occupato)
{
energy.GetComponent<Energyscript>().avaiable+= required;
print(energia.GetComponent<Energia>().disponibile);
Destroy(gameObject);
}
else
{
transform.position = new Vector3(slot.transform.position.x, slot.position.y, 0.0F);
slot.GetComponent<Slot>().occupato = true;
}
}
creato = false;
toccato = false;
}
}
}
Note that the block that must be spawned is in the resources folder (not in the scene). The generator and energy are both in the scene.
However: i run the game: everything is normal; energy is set at 10, same for required (in both of generator and the object to spawn)
i touch the generator, the object spawns and i drag the object into the scene; the console says to me that now energy is at 0, as expected; BUT THEN:
i drag the spawned block into the garbage icon: it gets destroyed and the log says to me that now energy is at 0; BUT if i click again the generator (and it's supposed to spawn another object since energy is again at 10), it just logs to me a "beep", just like i don't have anymore energy. In fact, if i watch the energy object in the inspector, it says to me that energy is at 0.
But now the 2nd weirdest thing: if i restart the scene (i have a button that if clicked it runs the code Application.LoadLevel(Application.loadedLevelName);), and drag again the object into the scene and then i drag the object into the garbage, it logs to me 20! And still can't spawn new object (and energy is still at 0); The same thing happens if i directly drag the object into the garbage immediately after it spawns.
The 3rd weirdest thing is that it continues to increase starting with the latest saved value (in that case 20) even if i actually kill the game (by pressing the play button) and restarting again, and it resets only if i close unity and open again it.
Since i really can't find a solution, could someone help me?
Answer by Xtro · Jul 30, 2013 at 05:50 PM
Aren't you supposed to decrease the enery in the first script ?
energy.GetComponent().avaiable -= required;
instead of...
energy.GetComponent().avaiable += required;
i really dunno how it happened but actually the line in my script had -=, not += (i edited the question)
However it doesn't solve my problem.
It's really hard to figure it out without seeing your setup. Can you create a smaller project(only adding these parts) and attach here ?
never$$anonymous$$g. i found the solution by myself. Basically the error occurred because i used as energy a prefab in the resource folder and not the actual gameobject in the hierarchy. I used ins$$anonymous$$d gameobject.find and now it works well. $$anonymous$$any thanks for your time spent trying to help anyway :D