- Home /
Problem with Instantiate and IF
Hi, I need your help because I can't really see what's wrong here. I want my script to check if "AttEnergy" is more than a particular number (let's say 101) and if it's that the case :
make a simple AttEnergy -= 100 (subtract 100 from the initial value)
Instantiate and make a copy of himself
My problem is that with my code the object copy himself but "forget" to subtract 100, the strange thing is that if I delete the instantiate line it does subtract 100. Can you help me understanding what's wrong and how to make it do both the operation ?
using UnityEngine;
using System.Collections;
public class CellController : MonoBehaviour
{
public int AttEnergy;
private int MaxEnergy = 200;
private float RegEnergy = 1;
private float EnergyAdder;
public GameObject ThingToBeCloned;
void Update()
{
if (AttEnergy < MaxEnergy)
{
EnergyAdder += RegEnergy * Time.deltaTime;
AttEnergy = (int)EnergyAdder;
}
if (AttEnergy >= 100)
{
AttEnergy -= 100;
CloneFunction();
}
}
void CloneFunction()
{
Instantiate(ThingToBeCloned,
transform.position,
transform.rotation);
}
}
where are you calling update()
from? unity won't call it automatically... it will if you change the name to Update()
;)
is AttEnergy a public member ? can you see it in inspector ? if not try doing that and if this is still the issue make sure you have not initialized this variable in start or onEnable because they will overwrite value as soon as object is created.
You really need to include the whole script. Is AttEnergy a static member, for example? If so, the newly instantiated class and the existing instance are going to be overwriting the same value.
Shouldn't you be substrating 100 to EnergyAdder ins$$anonymous$$d ? Because currently your AttEnergy just follows the value of EnergyAdder and each frame it resync with its value. I'm guessing it clones every frame, right ?
@garazbolg I thought that too. Since the value of EnergyAdder increases every frame, at some point, EnergyAdder > 100, at which point an extra clone is going to be created every frame.
@cocche to be honest, your code logic is a little confusing. What are AttEnergy, RegEnergy and EnergyAdder? Why is AttEnergy an int and the others floats? Can you describe in words what you expect to happen?
Answer by DiegoSLTS · Aug 23, 2016 at 01:21 PM
You're cloning 'ThingToBeClones' instead of the current GameObject, and you said it clones itself, so I guess there's one error. Also, you substract 100 from AttEnergy but not from EnergyAdder, so it'll grow forever, and at some point removing 100 won't be enough to set it below MaxEnergy. And you check against MaxEnergy first, and then against a hardcoded 100... That looks odd.
Your answer
Follow this Question
Related Questions
problem with instance 1 Answer
Clickable GameObjects within one another... 0 Answers
How can I assign a clone with a script to a variable? without drag & drop 1 Answer
my object instantiates to much 1 Answer
Moving a transform behaves odd 0 Answers