0

I'm writing a little text adventure for fun and I have a scene where the character is able to fish from a river. I'm trying to let him catch a few fish at a time but when I run the code the Random.Range method keeps going until it passes my threshold. I tried a few loops to see if I could get it to run once but no such luck.

I'm sure I'm missing something obvious but I keep running into the same problem. First I tried a for loop, the code below I used a do while loop but neither were effective. Thanks very much for your help.

Here's the code I'm having problems with:

public void catch_fish()
    {
        if (fish > 1000)
        {
            text.text = "Rock-Smasher already has a lot of fish! You should cook those first! \n\n " +
            "Press Space to back to the river";
            if (Input.GetKeyDown(KeyCode.Space))
            {
                currentState = States.river;
            }
        }
        else
        {
            int amount = 0;
            amount = Random.Range(0, 7);                
            fish = fish + amount;
            text.text = "You peer down into the water and you quickly thrust your hand into the water, catching " + fish + " fish. Wow! \n\n " +
                "Press R to go back to the river, or F to catch more fish";
            if (Input.GetKeyDown(KeyCode.R))
            {
                currentState = States.river;
            }
            else if (Input.GetKeyDown(KeyCode.F))
            {
                currentState = States.catch_fish;
            }
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • If you are using C# Random class you should be able to do something like this `Random rnd = new Random();amount = rnd.Next(1, 7);` – Alex W Apr 10 '17 at 01:18
  • See this: http://stackoverflow.com/questions/3975290/produce-a-random-number-in-a-range-using-c-sharp – Zac Faragher Apr 10 '17 at 01:19
  • 2
    `Input.GetKeyDown` looks like Unity3d, if this is Unity3d you must add the tag to your question as a edit. – Scott Chamberlain Apr 10 '17 at 01:38
  • What you want is a way for the game to wait until the user presses a button? This code will not achieve that. How many times catch_fish run? I don't see the purpose of your do while. It will not loop, right? dummy = 0, dummy++, while (1 < 1). What do you want to achieve? That is the important question here. "I'm trying to let him catch a few fish at a time but when I run the code the Random.Range method keeps going until it passes my threshold". The Random.Range has nothing to do with the execution of this method, it just sets a variable. – Fredrik Schön Apr 10 '17 at 08:40
  • I removed the loop, I had only set that to try and get catch_fish to run only once. The issue I'm having is that catch_fish continues to run until the amount exceeds the threshold and I'm not sure how to fix that so it only runs once. – Yearlongjester Apr 10 '17 at 15:19

1 Answers1

0

(Posted on behalf of the OP).

I guess using a random usually keeps going for whatever reason, but I just tied the RNG with an Input.GetKeyDown method and it'd producing the intended result now.

Servy
  • 202,030
  • 26
  • 332
  • 449
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Thank you, and sorry for the confusion. I'm still learning programming basics and I've never used Stackoverflow.com before. I appreciate everyone's help you all steered me to where I needed to go – Yearlongjester Apr 11 '17 at 22:32
  • No worries, thanks for wanting to post your answer @Year - helping future readers is what we're about here! – halfer Apr 11 '17 at 23:27