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 /
This question was closed Jun 21, 2018 at 05:19 PM by PourRine for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by PourRine · Jun 17, 2018 at 08:37 PM · bugcoroutinetimebooleansetactive

Boolean, IEnumerator and SetActive probleme.BUG

Hi, normally I always look in the forums before asking a question (it's even the first time I ask a question on this forum).

I have 2 scripts, the problem probably comes from the Shoot script because by removing the delay and the IEnumerator (and therefore the WaitForSecond) I can change side of my player and shoot as many times as I want to side but without delay so it pulls in Permanently, but when I put a delay (With a boolean and an IEnumerator) I can only change sides 3 times on each side and then I can not shoot anymore but only change sides. To change sides I use the SetActive function to do this.

Thanks to those who will help me because I do not understand the reason for this problem!

Player script+hierarchy+inspector;

 //Change side player shoot
     [SerializeField] int SelectedArm = 1;
     [SerializeField] int SelectedArmLeft = 1;
     [SerializeField] int SelectedArmRight = 2;
     public GameObject left;
     public GameObject right;
 
     void Start()
     {
         right.SetActive(false);
     }
 
     void FixedUpdate()
     {
         //Change side
         Side();
 
     }
 
 
     void Side()
     {
         //Change side of boat
         if (Input.GetKeyDown(KeyCode.E))
         {
             SelectedArm += 1;
             if (SelectedArm == SelectedArmLeft)
             {
                 left.SetActive(true); right.SetActive(false);
             }
             if (SelectedArm == SelectedArmRight)
             {
                 left.SetActive(false); right.SetActive(true);
             }
             if (SelectedArm > SelectedArmRight)
             {
                 SelectedArm = SelectedArmLeft; left.SetActive(true); right.SetActive(false);
             }
         }
     }

alt text

Shoot script+hierarchy+inspector;

     public Rigidbody rgBullet;
     public float impulseForce = 85;
     public float timeBetweenShoot = 0.8f;
     public bool canShoot = true;
     Transform spawn;
 
     void Start()
     {
         spawn = GetComponentInChildren<Transform>(); //Transform of canon child
 
     }
 
     void FixedUpdate()
     {
         if (canShoot == true)
         {
             StartCoroutine(Shoot());
         }
 
     }
 
     public IEnumerator Shoot()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             Rigidbody bulletInstance;
             bulletInstance = Instantiate(rgBullet, spawn.position, spawn.rotation) as Rigidbody;
             bulletInstance.AddForce(spawn.forward * impulseForce, ForceMode.Impulse);
 
             canShoot = false;
             yield return new WaitForSeconds(timeBetweenShoot); // wait 0.8 second befor next shoot
             canShoot = true;
 
         }
 
     }

alt text

I hope the solution is not simple because I will be an idiot ^^

fxwc.png (104.3 kB)
faeqdx.png (91.2 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by DawidNorasDev · Jun 19, 2018 at 03:14 PM

This code have many flows. 1. Don't do logic in FixedUpdate. Especially don't check there for Input. 2. Do not call coroutines in update just to check in them if there was an input 3. Note: Coroutines cannot run on disabled objects. Maybe you disable object, than coroutine wants to return and click "canShoot" to true, but it cannot, because object is disabled.

      [SerializeField] int SelectedArm = 1;
      [SerializeField] int SelectedArmLeft = 1;
      [SerializeField] int SelectedArmRight = 2;

Check out enums to get rid of this code with increments. It really doesn't help understand what is going on

Solution: You can try not to disable object, but to change public bool isNowShooting on them. And than in Update check for it. That way, you never disable it, and coroutine can finish.

Comment
Add comment · Show 1 · 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 PourRine · Jun 19, 2018 at 03:27 PM 0
Share

Thank you for the answer, I try that and I tell you if it works.

avatar image
0

Answer by PourRine · Jun 19, 2018 at 03:47 PM

Thanks a lot I understand better now why it does not work before, it works great thanks :)

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

Follow this Question

Answers Answers and Comments

157 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

Related Questions

I'd like my object to be active only when space key is NOT pressed 1 Answer

How can I use "animator.SetTrigger()" if "animator.SetBool()" has been true for X amount of seconds? 0 Answers

How to erase message after 3 seconds 1 Answer

My Coroutine Ignores the Boolean 1 Answer

Get Time A Bool Has Been True 4 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