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 $$anonymous$$ · May 17, 2016 at 11:42 PM · randomrandom.rangefunction updatetime.time

Lasers Fire over and over

I have a program that randomly chooses a number between 1 and 3, and therefore chooses an enemy to fire a laser. But sometimes when an enemy is picked, nobody else shoots but him. randomNumber doesn't change. Please Help! I have no idea and I've tired over this for days! P.S nextShot and period is my way of creating a pause in the update function. It might be the problem but once again, I don't know! P.P.S in another script are the commands on where the laser moves. @aldonaletto

 function Update () 
 {
     if(evilEntity1Fire == true)
     {
         if(Time.time > nextShot)
         {
           nextShot = Time.time + period;
           var laserPrefab1 : GameObject;
           laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
           evilEntity1Fire = false;
         }
         
     }
 
     if(evilEntity2Fire == true)
     {
         if(Time.time > nextShot)
         {
             nextShot = Time.time + period;
             var laserPrefab2 : GameObject;
             laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
             evilEntity2Fire = false;
         }
     }
 
     if(evilEntity3Fire == true)
     {
         if(Time.time > nextShot)
         {
             nextShot = Time.time + period;
             var laserPrefab3 : GameObject;
             laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
             evilEntity3Fire = false;
         }
     }
 
     if(randomNumber == 1)
     {
         Robot1();
     }
 
     if(randomNumber == 2)
     {
         Robot2();
     }
 
     if(randomNumber == 3)
     {
         Robot3();
     }
 }
 
 function Robot1 ()
  {
     yield WaitForSeconds (difficultyWait);
     evilEntity1Fire = true;
     randomNumber = Random.Range(1,3);
 }
 
 function Robot2 ()
 {
     yield WaitForSeconds (difficultyWait);
     evilEntity2Fire = true;
     randomNumber = Random.Range(1,3);
 }
 
 function Robot3 ()
 {
     yield WaitForSeconds (difficultyWait);
     evilEntity3Fire = true;
     randomNumber = Random.Range(1,3);
 }
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
0

Answer by Tohaveaname · May 18, 2016 at 02:10 PM

I don't use JavaScript so I apologise for any potential syntax errors on my part but try this:

 function Update () 
 {
      
     if(Time.time > nextShot) // Stopped checking for this multiple times as it was unecessary.
                              // Not entirely sure what it's doing but you said you need it for
                              // pause functionality so I left it alone.
     {
         nextShot = Time.time + period; // Again not entirely sure what this is doing 
                                        // but you said you needed it.
 
         if(evilEntity1Fire == true) // Changed to an else if block as it seems two robots 
                                     // can't shoot at the same time.
         {
             var laserPrefab1 : GameObject;
             laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
             evilEntity1Fire = false;
         }
          
         else if(evilEntity2Fire == true)
         {
             nextShot = Time.time + period;
             var laserPrefab2 : GameObject;
             laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
             evilEntity2Fire = false;
         }
         
         else if(evilEntity3Fire == true)
         {
             var laserPrefab3 : GameObject;
             laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
             evilEntity3Fire = false;
         }
     }
 
     Invoke("Robot", difficultyWait);
 }
  
 function Robot ()
 {
      randomNumber = Random.Range(1,4); // The documentation states "The returned value will 
                                        // never be max" so 3 wouldn't have ever occurred.
                                        // I also suspect this does not need to be a class 
                                        // variable just a local.
 
      if(randomNumber == 1)
      {
          evilEntity1Fire = true;
      }
      
      else if(randomNumber == 2)
      {
          evilEntity2Fire = true;
      }
 
      else if(randomNumber == 3)
      {
          evilEntity3Fire = true;
      }
 }

I have tried to tidy up your code as well. I hoped this helped.

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 $$anonymous$$ · May 23, 2016 at 08:48 PM 0
Share

Thanks for trying, Tohaveaname, but the problem is still there. Ive discovered that when I have the nextShot and Time.time stuff in the script, it does what I want in speed terms but after a while it eventually ends up with just the first evilEntity firing forever. If i get rid of the nextShot stuff, every evilEntity fires forever at a rapid pace. Thankyou for trying though

avatar image
0

Answer by $$anonymous$$ · May 23, 2016 at 09:02 PM

EDIT : I fixed it! Thanks Tohaveaname, your code was awesome. The problem that we both ovelooked was evilEntityfFire(x) not turning off. I just moved all the statements saying evilEntityFire(x) = False outside the if statements and just inside the Update() Function. Thanks so much for your help though Tohaveaname, that was the only thing I changed. Wooh! :D

P.S Thankyou

 function Update () 
  {
       
      if(Time.time > nextShot) 
      {
          nextShot = Time.time + period; 

          if(evilEntity1Fire == true) 
          {
              var laserPrefab1 : GameObject;
              laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
              
          }
           
          else if(evilEntity2Fire == true)
          {
              nextShot = Time.time + period;
              var laserPrefab2 : GameObject;
              laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
          }
          
          else if(evilEntity3Fire == true)
          {
              var laserPrefab3 : GameObject;
              laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
          }
      }
  
      evilEntity1Fire = false;
      evilEntity2Fire = false;
      evilEntity3Fire = false;
      Invoke("Robot", difficultyWait);
  }
   
  function Robot ()
  {
       randomNumber = Random.Range(1,4); 
  
       if(randomNumber == 1)
       {
           evilEntity1Fire = true;
       }
       
       else if(randomNumber == 2)
       {
           evilEntity2Fire = true;
       }
  
       else if(randomNumber == 3)
       {
           evilEntity3Fire = true;
       }
  }

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 Tohaveaname · May 24, 2016 at 07:26 AM 0
Share

I always overlook the simple stuff hahaha. Nice one.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Random.Range only returns lowest value 1 Answer

How can I randomly create a number and then delete it to stop repeating 3 Answers

Random between -1 and 1 2 Answers

Creating a record of values generated with Random.Range 1 Answer

Teleport to a random height 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