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 BlueBottom · Feb 17, 2015 at 08:07 PM · c#instantiateinputbooleanif

if function broken?

So, here's the code:

public class EP_Instantiate : MonoBehaviour {

 public Transform EP_fire;
 public Transform EP_earth;
 
 bool EP_fire_active;
 bool EP_earth_active;
      
      
 void Start() 
 {
      bool EP_fire_active = false;
      bool EP_earth_active = true;
 }
 
 
 void Update() 
 {
     if(EP_earth_active == false && Input.GetButtonDown("Jump")) 
     {
         Instantiate(EP_fire);
         EP_fire_active = true;
     
     }
 }

}

I trimmed some unused booleans to make the code more readable. In this situation, I'd expect that the prefab "EP_fire" would not instantiate. However, the prefab does instantiate on input, regardless. Can some one make sense of this?

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

3 Replies

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

Answer by Jessespike · Feb 17, 2015 at 08:14 PM

Inside of Start(). Try removing the bool keywords.

 void Start()
 {
      EP_fire_active = false;
      EP_earth_active = true;
 }
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
avatar image
1

Answer by tanoshimi · Feb 17, 2015 at 08:14 PM

You mean that EP_fire is being instantiated every frame? Or only when you press the Jump key?

If the second, that's expected behaviour. If the first, your Jump key is stuck.

I'm assuming #2, so to explain: EP_earth_active is false. You declare it on line 5 (actually line 6, but your code formatting is wrong) but don't give it a value, so it's initialised to false by default. bool EP_earth_active; Then, in Start(), you declare some more variables with the same name and initialise these to true, but these are locally-scoped only. Correct this by getting rid of your Start() function and instead assign a value to the bools on declaration:

 public class EP_Instantiate : MonoBehaviour {

 public Transform EP_fire;
 public Transform EP_earth;

 bool EP_fire_active = false;
 bool EP_earth_active = true;

... etc. etc.

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
avatar image
0

Answer by Owen-Reynolds · Feb 17, 2015 at 08:15 PM

If statements aren't broken. You made a regular C# programming mistake somewhere. Track it down the usual way.

Make an IF with each condition by itself, to test. A Unity trick is to make EP_xxx public, so you can see them change in the Inspector. Add print statements (printing out various values, if you need to.) I suspect you have some yellow triangle warnings. Those are often correct (they tell you why the programs runs the wrong way.)

Just going over each line (there aren't that many) you might even be able to spot the error.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Lightswitch help? 3 Answers

Input loss. One out of every five or six mouse clicks or button presses don't register. 1 Answer

Instantiating on Input 1 Answer

Questions of the Coroutines 1 Answer

Distribute terrain in zones 3 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