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 /
  • Help Room /
avatar image
0
Question by Noodles_yummy · Feb 01, 2018 at 03:42 AM · animationcollisionscript.scripting beginnerscriptingbasics

Set Bool to false after seconds?

Hi! I'm quite new to Unity, (well in my books, a few months + no training = new lol) therefore I'm not the best at coding, and I was wondering if I could get a few pointers. I have an animation for my player to raise a gun on cue (I can't get it back down, I have a bool parameter so that when I hit space the bool = true, and I couldn't get it back down again it kept giving me compiling errors) Also I've been trying to get the actual gunshot animation to work (I have it with another bool so when you hit "z", Bang = true but the issue is that it stays true (leaving you frozen with no animation) and I'm thinking I could use a timer so it becomes false before the animation is over so it just goes back to the idle animation, but I couldn't get that working either. I eventually got bored and focused more on art (I have glow effects and lens flares + my art it looks AWESOME) but you cant do much other than take out your gun and shoot once (But then you're stuck, frozen). I also had some dialogue working, but I must've messed up colliders and now the whole thing is just a mess with sitting code that doesn't do anything lol. No compiling errors (That make it unplayable anyway) and looks gorgeous. Anyway if someone could help me out in any small way I would be extremely grateful as this is my ultimate dream (not to be cheesy) but I've always wanted to do this and this is my first game. :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Carterryan1990 · Feb 02, 2018 at 01:50 AM

 bool bang;
 public float m_TimeUntilBang;
 
 void Start()
 { 
 Invoke("Onbang", m_TimeUntilBang);
 }
 
 void OnBang()
 {
    bang = !bang; 
 //Or it could be 
  bang = false;
 }
 Void YOURMETHOD()
 {
 Invoke("Onbang", m_TimeUntilBang);
  }
 }
 
  
 
 
 I rather use invoke its easier to set up and to me a more convenient way to use a timer.
 
 
 
 
 

bool bang; public float m_TimeUntilBang;

void OnBang() { bang = !bang; //Or it could be bang = false; } Void YOURMETHOD() { Invoke("Onbang", m_TimeUntilBang); } }

you could also call your method in the start function. so

void Start() { YOURMETHOD(); }

I rather use invoke its easier to set up and to me a more convenient way to use a timer.

Comment
Add comment · Show 3 · 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 Noodles_yummy · Feb 02, 2018 at 04:29 AM 0
Share

@Carterryan1990 Hi, this is my second time commenting, wifi screwed up last time lol. I don't know if it will post the other one eventually, but basically I said "It looks like it makes sense, but where would you write how long you have to wait?" and thanks for the reply btw ^^

avatar image Carterryan1990 Noodles_yummy · Feb 04, 2018 at 05:07 AM 0
Share

@Noodles_yummy No problem. The public float m_TimeUntilBang, Is your value. After creating the script and placing it on a scene object you will see in the inspector a public variable called m_TimeUntilBang with a text box to the right of it, where you would input a number. You could also do it like this, float m_TimeUntilBang = 3. Which would automatically set the float to 3.

When you start your scene if the method is called on start your bool would be called in 3 seconds. As you see in the script, i use Invoke("THE$$anonymous$$ETHOD", and then the value which is m_TimeUntilBang, which = 3. Invoke will call the method after real time has past your value. Say you set the value to 60, it would take 60 seconds until the method is called.

Can i ask what exactly you are trying to do and ill write the script for you and explain to you how i did it. thanks!

avatar image mattis89 · Sep 24, 2018 at 09:34 PM 0
Share

hmm... this wont work?.. $$anonymous$$Y bool is still true..

avatar image
1

Answer by Nighfox · Feb 01, 2018 at 04:14 AM

You might want to post some of your animation code and your Animator controller setup. This might be either a logic issue or a transition problem.

Comment
Add comment · Show 17 · 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 Noodles_yummy · Feb 01, 2018 at 04:55 PM 0
Share

@Nighfox Heya! Thanks for the reply, here is my animator and a few lines of code, hope this makes sense lol. Like I said I'm pretty new to this. alt text

animator.png (63.4 kB)
helpcode.png (78.0 kB)
avatar image Noodles_yummy · Feb 01, 2018 at 05:00 PM 0
Share

Hmm..it's not letting me post the third picture so I just copied and pasted my code! This will look extra messy, sorry about. //me trying things out, like I said it's pretty messy lol. This wouldn't work cause gunpoint was already true. if (gunpoint = true) if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) anim.SetBool("gundown", true);

     //just movement, it works just fine
     Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

     //ignore this, just movement some more
     rb2d.AddForce (movement * speed);
 }
 
 //I took some stuff off the internet and tried to get it working, I don't know why it's not, I'm not getting any compiling errors... hmm...
 void Foo() { StartCoroutine(Begin()); }

 IEnumerator Begin()
 {
     if (BANG = true)
     yield return new WaitForSeconds(3f);

     BANG = false;
     // Code here will be executed after 3 secs
     //Do stuff here

Thanks again!

avatar image TreyH Noodles_yummy · Feb 01, 2018 at 05:15 PM 1
Share

What' s calling Foo() ? Also, it's worth noting that regardless of your value for BANG:

 if (BANG = true)
 {
     // something
 }

will always run. That line sets BANG to true and then the if statement evaluates that line as true. I think you want:

 if (BANG == true)
 {
     // something
 }


avatar image Noodles_yummy TreyH · Feb 01, 2018 at 08:54 PM 1
Share

@TreyH Hey! Thanks for the reply! I think foo is just something the people who made the code have specific to their script and I just accidentally kept it in there lol. And I'll try that out, but what's the difference between = and ==? Thanks again ^^

Show more comments
Show more comments

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

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

Clarifaction needed on LoadLevel delay. 1 Answer

help for C# ; when all were green 1 Answer

Hi, we are currently doing our thesis, it's a quiz type of game. My question is, how do I collect the scores from four different scripts and output them on the menu page? 1 Answer

How do I read this? private Rigidbody rb; 2 Answers

How To Add Animations Through C# Scripts 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