- Home /
Random picture picker
I'm new to Unity and what i'm trying to do is have a button which will keep going through several pictures then pick one like a slot machine. Depending on what picture you get the game will let you do something different. For example if you get a movement picture lets you move, if you get a attacking picture you can attack. Not too sure where to start with this.
Something along these lines
static var RandomNum: int; var DiceSound : AudioClip;
function Update(){ if(Input.Get$$anonymous$$eyUp("r")){ RandomNum = Random.Range(1,6); } if(RandomNum == 1){ audio.clip = DiceSound; audio.PlayOneShot(DiceSound); } }
Then do something else if its another number.
What is it exactly you aren't sure about?
Besides, whouldn't it be better to play the sounds inside the >>if(Input.Get$$anonymous$$eyUp("r")<
Ah sorry it all got squashed together. The audio was put into that bit to see whether it was working. I'm getting a error when I press R. "$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'Random.Range'. How would I make it display the associated picture with the number it generates?
Answer by Rod-Green · Dec 26, 2011 at 09:38 PM
Firstly you might need to think of more like random 'card' picker rather than just a picture or texture. Semantics I know but it's good to think about it as game concept rather than a literal asset concept.
So with that said how about creating this:
using System.Collections.Generic;
using UnityEngine;
public enum GameCardType
{
MOVEMENT,
OTHER_TYPES_HERE,
ENUM_COUNT
}
public class GameCardGame : MonoBehaviour
{
public bool m_playGame;
public Texture2D[] m_cardTextures; // assign matching textures to this property
public GUITexture m_cardGUITexture;
//Assigned random card
private GameCardType m_curType;
//Picks a random card type
static GameCardType GetRandom()
{
return (GameCardType)Random.Range(0, (int)GameCardType.ENUM_COUNT);
}
//Movement code
void DoMovementType()
{
Debug.Log("Movement code!");
}
void DoOtherType()
{
Debug.Log("Other type code!");
}
//Plays the game
void PlayGame()
{
// Get a random card
m_curType = GetRandom();
//if not null and texture exists - assign the selected type to GUITexture
if(m_cardTextures != null && (int)m_curType < m_cardTextures.Length
&& m_cardGUITexture != null)
m_cardGUITexture.texture = m_cardTextures[(int)m_curType];
// Do random card logic
switch (m_curType)
{
case(GameCardType.MOVEMENT):
DoMovementType();
break;
case(GameCardType.OTHER_TYPES_HERE):
DoOtherType();
break;
default:
Debug.LogWarning("No method specified for card type!");
break;
}
}
//Update loop to catch the m_playGame bool trigger
void Update()
{
if(m_playGame)
PlayGame();
m_playGame = false;
}
}
Woah thanks for all that i'll try getting it working, gotta work my head around it first :)
Your answer
Follow this Question
Related Questions
java scrip, dice throw at a random rotation and force? 1 Answer
Randome Number for Dice 2 Answers
How do you script to show random image for seconds? 2 Answers
Dice Animation and Face Determination 7 Answers
Creating a light in game C# 1 Answer