Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 cocche · Aug 23, 2016 at 07:40 AM · instantiatecloneifif statement

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 :

  1. make a simple AttEnergy -= 100 (subtract 100 from the initial value)

  2. 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);
             }
 }

Comment
Add comment · Show 10
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 gjf · Aug 23, 2016 at 01:36 AM 1
Share

where are you calling update() from? unity won't call it automatically... it will if you change the name to Update() ;)

avatar image _Yash_ · Aug 23, 2016 at 09:16 AM 1
Share

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.

avatar image tanoshimi · Aug 23, 2016 at 09:55 AM 1
Share

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.

avatar image Garazbolg · Aug 23, 2016 at 10:31 AM 1
Share

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 ?

avatar image tanoshimi · Aug 23, 2016 at 10:38 AM 1
Share

@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?

Show more comments

1 Reply

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

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.

Comment
Add comment · 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

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

7 People are following this question.

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

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


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