Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Turkeybag · Sep 27, 2013 at 11:04 AM · clonestatic variablestatic-vars

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?

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Sep 27, 2013 at 11:11 AM 0
Share

Please show code you're using for destroy and decrease count.

avatar image Fattie · Sep 27, 2013 at 01:04 PM 0
Share

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

avatar image Jamora · Sep 27, 2013 at 01:11 PM 0
Share

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 >();
avatar image Turkeybag · Sep 27, 2013 at 01:55 PM 0
Share

@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

avatar image ArkaneX · Sep 27, 2013 at 02:15 PM 0
Share

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...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Sep 27, 2013 at 04:11 PM 0
Share

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!

avatar image Turkeybag · Oct 10, 2014 at 01:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges