Recent Posts

Blogroll

Friends

Music

News

Philanthropy

Archives


« | Main | »

Unity3d and Random Number Generators (GUI)

By Billy | November 10, 2012

I’ve started messing around with Unity3d and programming in C# and Javascript. I can’t find ANYTHING worth while to get me started on Boo, so I’m stuck ignoring what could be potentially be a great language for me to program in. Anyway, I had to piece together a few things in order to write this random number generator recipe, so I thought I’d share it in case anyone wanted to learn the same baby steps as me.

Unity’s general discussion points on randomness leave the beginner programmer (me, for instance) a little disappointed.

In Python 2.7, one can simply type:

import random
print random.randrange(0,10)

and they will get a pseudorandom number between 0 and 10 ,extremities inclusive.

It’s my understanding that in unity, as with LSL for Second Life, once cannot execute code without first inserting it into some object. That mean’s Descarte’s Dreamer Scenario logic (cogito ergo sum, which I personally find bafflingly illogical) holds for programming in Unity3d. If there’s code executing, it’s certain there’s at least a cube there to think it.

I’m interested in eventually making a more-than-basic GUI, so the following recipe produces a GUI with a single button, whose sole purpose is to print a random number. This is not the most simple way to do it, but I do so love the way Unity allows you to mess around with variables on the fly, so I almost never bury variables in the code.


#pragma strict
//RandomNumberGenerator.js

//Random Number Variables:
var biggestNumber: int = 100;
var smallestNumber: int = 0;

//GUI Button Variables:
var buttonWidth : int = 500;
var buttonHeight : int = 200;

static var randomChance: int;

function OnGUI(){
if(GUI.Button(Rect(Screen.width/2 - buttonWidth/2,Screen.height/2 - buttonHeight/2,buttonWidth,buttonHeight), "Generate a random number!")){
var randnum = (Random.Range(smallestNumber,biggestNumber));
print (randnum);
}
}

Apparently I don’t know how to make pretty looking code appear on my blog, so I’ll work on that in the future.

The code is simple. Define a variable range of numbers, create a variable size of GUI button. I’m not sure how to explain the static variable randomChance, because I stole it from some youtube video, but it works. [EDIT: I just got rid of randomChance because I never call it!] The only function in the code basically says, “Hey, we’re using a GUI. There’s a button with the string “Generate a random number” in it, and when you click on that, do this: call randnum a random number in between our previously established max/min, and print it out.

The crap right after GUI.Button(Rect( basically centers the button. I haven’t done the calculus to figure out if it’s perfect, but for now I like it. I think there are also ways to center it without so many fractions (using GUI Styles), but this is what I’m working with right now.

If you’re reading this whole thing for some reason and have no idea what Unity is, it’s a free game developing engine that you probably use to play games on your iPad. You can download it from the Unity3d official website by using context clues to find it yourself on the internet.

ta dah

I know, it's too exciting to handle.

Topics: Uncategorized | Comments Off on Unity3d and Random Number Generators (GUI)

Comments are closed.