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 Arcticstar · Jul 14, 2017 at 09:05 AM · unity 5scripting problemphysicsscript.

HELP! Scripts looks fine but doesn't respond?

first of all here is the script

 public Text Redtext;
 public Text Bluetext;
 public Text Bigtext;

 bool RedisOn;
 bool BlueisOn;

 public float delayTimer;
 public float spwanMostwait;
 public float spwanlesswait;
 float timer;

 int RedScore;
 int BlueScore;


 void Start () {

     StartCoroutine (Spwaner ());

     timer = delayTimer;

     RedScore = 0;
     BlueScore = 0;


 }

 // Update is called once per frame
 void Update () {

     Redtext.text = " " + RedScore;

     Bluetext.text = " " + BlueScore;

     delayTimer = Random.Range (spwanlesswait,spwanMostwait);


     timer -= Time.deltaTime;

     if (timer <= 0) {

         if (delayTimer < 10) {
             RedText ();
             timer = delayTimer;
         }

         if (delayTimer > 11) {

             BlueText ();
             timer = delayTimer;
         }
     }



     }





 public void RedText (){

     Bigtext.text = "Red";
     RedisOn = true;
     BlueisOn = false;

 }


 public void BlueText(){

     Bigtext.text = "Blue";
     RedisOn = false;
     BlueisOn = true;


 }


 void OnCollisionEnter2D (Collision2D col) {

     if (RedisOn == true && BlueisOn == false) {
         if (col.gameObject.tag == "Red") {
             RedScore += 1;
         }
         if (col.gameObject.tag == "Blue") {
             Destroy (gameObject);
         }

     }
     if (BlueisOn == true && RedisOn == false) {
         if (col.gameObject.tag == "Blue") {
             BlueScore += 1;
         }
         if (col.gameObject.tag == "Red") {
             Destroy (gameObject);
         }
     }
 


 }

 IEnumerator Spwaner() {

     while (true) {

         yield return new WaitForSeconds (delayTimer);
     }
 }

my problem is the "BigText" doesn't show , which means that the delayTimer doesn't respond.

but when I change the delaytimer to timer in the if (delayTimer < 10) line, it works but I get no Random, I tried everything here but no solution so how I make this work so I get random text shown.

Comment
Add comment · Show 2
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 SohailBukhari · Jul 14, 2017 at 09:35 AM 0
Share

in the start you are assigning timer = delayTimer; and you are decreasing timer in the update, you are checking whether timer value less then or equal to zero, let suppose you have timer value 5 then condition never gets true because 5 is not less then or equal to zero. i think you should check greater then zero ins$$anonymous$$d of less then zero. Because your both conditions are inside timer less then zero bracesOther thing is that you are taking time as integer in the condition you should check timer <= 0.0f, never the value of timer be equal to zero.

avatar image SohailBukhari · Jul 14, 2017 at 09:49 AM 0
Share

lots of confusions in your code logic, these conditions never comes true according to your logic

  if (delayTimer > 11)
             {
                 BlueText();
                 timer = delayTimer;
             }

Because you assign again timer = delayTimer; in the above condition. Other thing is you are choosing delaytimer randomly and checking hard code values in the conditions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 14, 2017 at 10:01 AM

What values have you set for all your variables? Especially "spwanlesswait" and "spwanMostwait"?

You also have a dead-zone in your "delayTimer" range. If the value is between 10 and 11 nothing will happen. Though since you would constantly roll a new value it's probably not a problem.

Next weird thing is, why do you use Random.Range outside of the if(timer <= 0) check? You only need a new random number when you enter that block.

Finally a logical problem. You use the delayTimer value to reset your timer. However "red" is only choosen when delayTimer is "small" (less than 10). "Blue" is only choosen when delayTimer is "large" (greater than 10 or 11). That means on average the most time the color will be Blue as blue always gets the longer wait times. This seems very weird.

Finally what is the "Spawner" coroutine good for? At the moment it does nothing besides waiting. Since you change "delayTimer" constantly it's quite unclear for how long the coroutine waits. It certainly is not in sync with your "timer".

You probably want something like this:

 timer -= Time.deltaTime;
 if (timer <= 0)
 {
     timer = Random.Range (spwanlesswait, spwanMostwait); // set a new random wait time
     if (Random.value < 0.5f) // 50:50 chance to pick either red or blue
     {
         RedText ();
     }
     else
     {
         BlueText ();
     }
 }

ps: you have a typo in your "spwanlesswait" and "spwanMostwait" variables. You also use inconsistent casing. They probably should be "spawnLessWait" and "spawnMostWait".

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 Arcticstar · Jul 14, 2017 at 11:46 AM 0
Share

I solved it in a different way, I add an invokerepeating to repeat a Random method and the Red and Blue text will set active at random time set by the Random function. , . void Start () {

     InvokeRepeating ("random", 5, 5);
 

     RedScore = 0;
     BlueScore = 0;


 }

 public void random () {

     rand = Random.Range (0, 10);
         

 }

 // Update is called once per frame
 void Update () {


     Redtext.text = " " + RedScore;

     Bluetext.text = " " + BlueScore;


     if (rand < 5) {
         RedText ();
             
     }

     if (rand > 5) {

         BlueText ();
             
     }
 }

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

197 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

Related Questions

Using a Burst in a script 3 Answers

How can i convert this javascript line to csharp ? 1 Answer

[Shooting shots Scripting] How to make the compiler know that I mean the empty quad by the Public Transform? 0 Answers

RigidBody.velocity or transform.position 1 Answer

Playmaker: How to use and get variables from other visual scripting or C#? 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