Simple Rating System

Hello,

I want to implement a rating system where a user can rate something out of 5 in a GUI. For example, it can be toggles numbered 1 to 5 where you can select a number and hit a button to submit the rating. This rating will then be stored somewhere and the updated rating will be returned to the user.

Is this possible? I’m not very good with scripting to figure it out. What do I need to use to achieve it?

Many thanks.

Is this what you are looking for? It is basically created using the GUI.Toggle function, read more about it here:

var toggle1:boolean = false;
var toggle2:boolean = false;
var toggle3:boolean = false;
var toggle4:boolean = false;
var toggle5:boolean = false;
var stars:int;

function OnGUI () {
//GUI.Toggle(Rect(x position on screen, y position on screen, width of toggle, height of toggle), boolean value, "name of toggle")
	toggle1 = GUI.Toggle (Rect (100, 25, 25, 25), toggle1, "1");
	toggle2 = GUI.Toggle (Rect (125, 25, 25, 25), toggle2, "2");
	toggle3 = GUI.Toggle (Rect (150, 25, 25, 25), toggle3, "3");
	toggle4 = GUI.Toggle (Rect (175, 25, 25, 30), toggle4, "4");
	toggle5= GUI.Toggle (Rect (200, 25, 25, 25), toggle5, "5");
	
	if((toggle1 == true   stars != 1)){
		toggle2 = false;
		toggle3 = false;
		toggle4 = false;
		toggle5 = false;
	}else if((toggle2 == true  stars != 2)){
		toggle1 = false;
		toggle3 = false;
		toggle4 = false;
		toggle5 = false;
	}else if((toggle3 == true  stars != 3)){
		toggle1 = false;
		toggle2 = false;
		toggle4 = false;
		toggle5 = false;
	}else if((toggle4 == true  stars != 4)){
		toggle1 = false;
		toggle2 = false;
		toggle3 = false;
		toggle5 = false;
	}else if((toggle5 == true  stars != 5)){
		toggle1 = false;
		toggle2 = false;
		toggle3 = false;
		toggle4 = false;
		toggle5 = true;
	}
	NumberOfStars();
}

function NumberOfStars(){

	if(toggle1 == true){
		stars = 1;
	}else if(toggle2 == true){
		stars = 2;
	}else if(toggle3 == true){
		stars = 3;
	}else if(toggle4 == true){
		stars = 4;
	}else if(toggle5 == true){
		stars = 5;
	}
	
	print(stars);
}

The easiest way to do this would probably be with a selection grid control:-

var options: String[];  // The five strings for the rating options.
var gridRect: Rect;

var selected: int;


function OnGUI() {
	selected = GUI.SelectionGrid(gridRect, selected, options, 1);
}

Yes, that would be much easier :slight_smile: …I’m not that familiar with seleciton grid, so I went with what I knew - though I had a feeling there had to be a simpler/less wordy way to do things.

Thanks, I got the GUI working now. But how can I get it to save the rating, say for example in a database, and every time you vote it adds to the database and prints out the current vote? Kind of like a typical 5 star rating system. Is that difficult to do?

Maybe I don’t need to use a database. Could I store the data in PlayerPrefs?