- Home /
Clone Objects treating static variables strangly
Hey guys! I have an enemy that will duplicate itself every now and then. Whenever one is created I have a static variable "DupeMobCount" that increases by 1, and when they are destroyed, it decreases by 1. Whenever one of these enemies is created, it gains health equal to DupeMobCount 2. Now as you can imagine, them always duplicating leads to all different amounts of "(Clone)" on the enemies. Some say it once, others 5 times. This is where things start to get really strange. If I don't kill any everything works as expected. They gain DupeMobCount 2 in health. However if I kill a few, they start to gain additional health bases on the number of "(Clone)"s they have for no apparent reason. There seems to be no pattern to it. Here's the code (Everything you don't need to see removed):
public static float DupeMobCount = 0;
//I dragged the whole prefab with the script on it into Dupe, and it works fine apart from the above issue
public MobStats Dupe;
void Start (){
DupeMobCount ++;
}
if (NextDupe <= Time.time && DupeMobCount <= DupeCap)
{
NextDupe = Time.time + DupeTime;
//MobStats is the script that stores mob health
MobStats Duped = Instantiate(Dupe, VarTransform.position, VarTransform.rotation) as MobStats;
Duped.Health += HealthPerDupe*DupeMobCount;
}
Also it might be important to mention these two things:
1) If I move the health gain into the start function then it seems that the health gain gets increased more and more for each (Clone) the object has rather than at a steady rate.
2) They also gain speed, damage and so on from DupeMobCount and that has no issues (it's calculated in update or DamagePlayer function, so pretty much when needed rather than at the start).
I have no idea what the problem is, why it's happening, or how to fix it. Anyone have any ideas?
Please show code you're using for destroy and decrease count.
there's a lot to be said for never using static. (particularly if a beginner)
just have a game object called "the big boss" or something and go there for your totals or whatever it is. Hell -- just use PlayerPrefs (it's no more or less efficient, why not do it)
most beginners are just using a static as a crutch for not hooking up to another object
What strikes my eye is that you're instantiating your Object as $$anonymous$$obStats. What happens if you keep a reference to the GameObject storing the script, ins$$anonymous$$d of the script itself?
public GameObject Dupe;
//on line 13
$$anonymous$$obStats Duped = ((GameObject)Instantiate(Dupe, VarTransform.position, VarTransform.rotation)).GetComponent< $$anonymous$$obStats >();
@ArkaneX. Thought I included it sorry :S $$anonymous$$ust have missed it
void OnDestroy (){
Dupe$$anonymous$$obCount --;
}
That's part of the same script and in another $$anonymous$$obStats it has this
public void Damage$$anonymous$$ob (float Damage){
Health -= Damage;
if (Health <= 0f)
{
Destroy(gameObject);
}
}
That's called by the player shooting at him.
@Fattie I just assumed that static variables worked but I guess there's no harm in having a master script or something do it. Although I though finding the master script every time one of these mobs is spawned would slow it down a little (50 spawning at the same time has happened in rare cases where the player let the mobs get out of hand :P)? Guess I'll have to use it if Jamora's idea doesn't work. EDIT: No difference :S
@Jamora I had it as transform one time and it did the same thing. I read somewhere that it didn't matter what you used as it would just create the whole prefab so using the script just made it easier if you needed to edit it. I'll try your suggestion and see if it helps. EDIT: No difference :S
Could you try to change this line:
Duped.Health += HealthPerDupe*Dupe$$anonymous$$obCount;
to
Duped.Health = HealthPerDupe*Dupe$$anonymous$$obCount;
If this helps (i.e. each new object has health equal to number of current objects x2), then maybe you modify the Health variable of original prefab somewhere?
EDIT: or you could simply do
print(Duped.Health);
just after instantiating...
Answer by Owen-Reynolds · Sep 27, 2013 at 04:00 PM
You shouldn't be getting an object that ends with two or more (Clones)'s. That means you're copying an existing gameObject instead of a prefab; which copies all of the current colors, scale, variables, isKinematic settings ... on that one object.
In your case, you got a copy with already increased health, which you increased even more. As the game gets more complicated, more things will break until you fix the double-clones.
I don't see what's wrong. Maybe click Dupe
in the Inspector to double-check it really is the prefab (the link-to object should puff yellow.) Maybe debug dump some stuff after each Instantiate. You only sometimes get (Clone)(Clone)? Maybe you have some Instantiate somewhere else, not using the prefab?
Also, your use of static for instance-counting is literally textbook. Lots of unity-users misuse static, and in general "get rid of the statics" is good advice. But to me dupeMobCount
is perfect just where it is.
I believe OR has identified the key problem "That means you're copying an existing gameObject ins$$anonymous$$d of a prefab" - spot of the day
"your use of static for instance-counting is [good]" ... if Owen says so, fair enough. (I don't have the time to read questions! :) ) Still, I have to say (a) evidently the OP simply does not understand statics. For my simple $$anonymous$$d, that's pretty much a 100.00% indication to "not use static". (b) if the OP is this muddled-up over something so simple he shouldn't be using them and (c) I have to say it is far better pedagogy to not use static, i.e. to actually read and learn the two most absolutely fundamental pages presently in literally the entire computer game industry, heh!...
http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html
http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
None of this would have even come up if OP had taken a day to do that. He/she would confidently have made some sort of holder game object, with a counter script on it or the like and everything would be clear and in place. Ins$$anonymous$$d we have a spaghetti situation!
That explains why it was happening! I would have been copying from the copy. $$anonymous$$ust have stopped checking this question after I got it 'working' from what ArkaneX said. Only now looking back on everything do I truly see my noobish ways. I guess that means unity answers has definitely been $$anonymous$$ching me and for that I thank you all!
Your answer

Follow this Question
Related Questions
Deleting an instance 2 Answers
How do I properly duplicate an object in a editor script? 3 Answers
I dont want "(clone)" but how to change? 2 Answers
MemberWiseClone Protected?!? 1 Answer
save/load cloned game object 1 Answer