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 Zitoox · Oct 19, 2016 at 10:32 PM · script.enable and disable script

Enable and Disable a plane

Okay, so, recently i made a script to enable an image when i hit it's collider. The script's function was to pause the game, and show the image. If the player pressed "P", then the game would unpause and the image would not be there anymore.

So, as i am making a FPS game and i wanted to change my current combat system, i was thinking "why not?" so i kinda updated the script to to the following:

I wanted to make my script DISABLE a plane (that is a muzzleflash) when the player started the game, and then ENABLE when the player pressed the left mouse button, wait for let's say 1 second and then disable it again. This would make a flash screen, and it would be easy to make.

But i am stuck! I don't know what i did wrong, but it simply doesn't do anything. It sucessfuly disable the plane in the start of the game, but it doesn't enable and disable it like a flash when i press the left mouse button =(

Here's the script (Java)

 #pragma strict
 
 var MuzzeFlash : GameObject; //The plane OR muzzeflash.
 public var FireRate : float; //A variable to determine the time to wait before disabling it.
 
 function Start () //This function will make the quad be disabled in the start of the scene.
 {
     MuzzeFlash.SetActive (false);
 }
 
 
 
 function CoUpdate(): IEnumerator //The only thing i could find that would fit was this.
 {
     if (Input.GetButton("Fire1")) {  //On left mouse click:
 
         MuzzeFlash.SetActive (true); //Enable the plane?
         yield WaitForSeconds (FireRate); //Wait for the seconds in the variable.
         MuzzeFlash.SetActive (false); //Disable the plane?
     }
 }
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

1 Reply

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

Answer by SmomoGame · Oct 20, 2016 at 04:45 AM

  function Update()
  {
      if (Input.GetButtonDown("Fire1")) {  //On left mouse click:
             CoUpdate() ;
          
      }
  }
 
 function CoUpdate(){
          MuzzeFlash.SetActive (true); //Enable the plane?
          yield WaitForSeconds (FireRate); //Wait for the seconds in the variable.
          MuzzeFlash.SetActive (false); //Disable the plane?
 }

Do not call coroutine every frame, or these coroutines will execute at same time, and the plane will act weirdly.

Use GetButtonDown instead.

But I think the point is that CoUpdate function NEVER execute because you NEVER call it.

CoUpdate not means "This is a coroutine version of Update function". Just a custom function. By the way, Update CANNOT be coroutine.

In javascript, you don't need to add IEnumerator, compiler will do it for you.

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 Zitoox · Oct 20, 2016 at 02:41 PM 0
Share

It kinda works. I said kinda because it is completely ignoring my FireRate variable and it disable the muzzle flash only 4 seconds after even if i change it, but i am going to accept your answer because it solved the main issue.

avatar image Zitoox Zitoox · Oct 20, 2016 at 02:51 PM 0
Share

Never$$anonymous$$d, i fixed it. Thanks!

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

59 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

Related Questions

How do I enable my scripts on default?? 3 Answers

Disable FirstPersonController.cs when I click on a object, then re-enable it when I hit Q 1 Answer

How to make Animation and Physics work together? 0 Answers

What the variable motorSetter and motor type should be ? 1 Answer

How can i add a second camera that will be showing only specific gameobject in game window ? 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