x


Random.Range(int, int) returning only min value

After having had no issue with Random.Range for the last few months, it suddenly has decided to stop working for me.

int random = UnityEngine.Random.Range(1, 4);

Is returning 1, and ONLY 1.

Here's the full code for context:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ForgeSprite : TimelineSprite
{
    protected static ForgeSprite mForgeSprite;

    static string mNextAnimation = "";

    Timeline mCommonTimeline = null;
    Timeline mFinishTimeline = null;
    bool mIsFinished = false;

    protected override void Start()
    {
       base.Start();

       Common.hideObject(transform);

       mForgeSprite = this;

       mCommonTimeline = getTimeline("Forge_Common");
    }

    public override void Update()
    {
       if (mNextAnimation != "")
       {
         if (mCommonTimeline != null && mCommonTimeline.isFinished())
         {
          int random = UnityEngine.Random.Range(1, 4);

          mNextAnimation += random;

          mFinishTimeline = getTimeline(mNextAnimation);
          mForgeSprite.playAnimation(mNextAnimation);

          mNextAnimation = "";
         }
       }

       if (mFinishTimeline != null)
       {
         if (mFinishTimeline.isFinished())
         {
          mFinishTimeline = null;

          mIsFinished = true;


         }
       }

       base.Update();
    }

    protected override void OnDestroy()
    {
       mForgeSprite = null;

       base.OnDestroy();
    }

    public static ForgeSprite getForgeSprite()
    {
       if (mForgeSprite == null)
       {
         mForgeSprite = Common.getNewObject("Effects/CutIns/ForgeAnimation").GetComponent<ForgeSprite>();
       }

       return mForgeSprite;
    }

    public static void playSuccess()
    {
       getForgeSprite();

       if (mForgeSprite != null)
       {
         Common.showObject(mForgeSprite.transform);

         mForgeSprite.mIsFinished = false;
         mForgeSprite.mFinishTimeline = null;

         mForgeSprite.playAnimation("Forge_Common");
         mNextAnimation = "Forge_Success_" ;
       }
    }

    public static void playFail()
    {
       getForgeSprite();

       if (mForgeSprite != null)
       {
         Common.showObject(mForgeSprite.transform);

         mForgeSprite.mIsFinished = false;
         mForgeSprite.mFinishTimeline = null;

         mForgeSprite.playAnimation("Forge_Common");
         mNextAnimation = "Forge_Miss_";
       }
    }

    public static void load()
    {
       getForgeSprite();
    }

    public static bool isPlaying()
    {
       if (mForgeSprite != null)
       {
         return mNextAnimation != "" || mForgeSprite.mFinishTimeline != null || mForgeSprite.mIsFinished;
       }

       return false;
    }

    public static bool isFinished()
    {
       if (mForgeSprite != null)
       {
         return mForgeSprite.mIsFinished;
       }

       return false;
    }

    public static void clicked()
    {
       if (mForgeSprite.mIsFinished)
       {
         mForgeSprite.mIsFinished = false;

         Common.hideObject(mForgeSprite.transform);
       }
    }
}

Any ideas on what I'm missing here?

more ▼

asked Mar 29 '12 at 10:56 PM

sharsnik gravatar image

sharsnik
106 18 25 28

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Unless you're constantly setting Random.seed to the same value every frame somewhere, that will return 1, 2, or 3.

more ▼

answered Mar 29 '12 at 11:50 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

Try that little script, so you can see it's not a trap :

for(int i = 0; i < 1000; i++ ) print( Random.Range( 1, 4 );

If that prints 1000 "1", I'll eat my hat. Luckily for me, I don't own one :p

Mar 30 '12 at 12:01 AM Berenger

Ah.. I was, in effect, setting the random seed each frame.

Mar 30 '12 at 01:14 AM sharsnik
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x599

asked: Mar 29 '12 at 10:56 PM

Seen: 1292 times

Last Updated: Mar 30 '12 at 01:17 AM