- Home /
Randomize texture within prefab
Hello everyone, I decided to try and make a simple memory game where you match the cards. I have not used Unity or done any scripting since college but I thought I would try anyway. So I found a tutorial on youtube and have been following along, however I decided to create my own assets instead of using the ones they provided, and now I am stuck.
In part of the tutorial, they write the script that will randomize the texture on the card...My problem is that my 'card' is not one single object/model. It is a cube made up of quads ( I am trying to do this quickly so I didn't have time to uv map my images to a normal cube and had to use quads so I could have a bottom and top, etc)
So what needs to happen is I somehow create a line of code that will tell Unity to randomize the texture on only the 'Top' portion of my quad-cube prefab. I don't know how to tell the script to go inside the prefab and change just one part of it.
This is a screen cap of my workspace, showing the particular quad that I want to have a randomized texture: http://iron-zing.deviantart.com/art/Screen-Shot-2014-06-19-at-4-55-39-PM-462182228
And here are a few of my current scripts:
This one is attached to the playmat:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Playmat : MonoBehaviour
{
public GameObject cardPrefab;
public Layout layout;
public GameObject board;
public List<Card>cards=new List<Card>();
// Use this for initialization
void Start ()
{
GameSettings.Instance ().SetDifficulty (GameSettings.GameDifficulty.Medium);
CreateLayout ();
CreateCardsFromLayout ();
CreateCardTypes ();
}
void CreateLayout()
{
board = layout.GetRandomLayout ();
}
void CreateCardsFromLayout()
{
foreach (Slot s in layout.slots)
{
GameObject go = GameObject.Instantiate(cardPrefab,s.transform.position,Quaternion.Euler(180,-180,360)) as GameObject;
go.transform.parent = s.transform;
Destroy (s.GetComponent<BoxCollider>());
cards.Add (go.GetComponent<Card>());
}
}
void CreateCardTypes()
{
for(int i=0;i< cards.Count/2;i++)
{
Card c1 = cards[i];
Card c2 = cards[cards.Count/2 - 1 - i];
string type = GameSettings.Instance ().GetRandomType ();
c1.GenerateCard(type);
c2.GenerateCard(type);
}
}
// Update is called once per frame
void Update () {
}
And this one is attached to the card prefab
using UnityEngine;
using System.Collections;
public class Card : MonoBehaviour
{
public string CardType;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void GenerateCard(string cardType)
{
CardType = cardType;
renderer.material.mainTexture = Resources.Load ("Graphics/" + CardType) as Texture2D;
}
void OnMouseDown()
{
print ("I'm Clicked");
}
}
I'm sure it's a simple solution but I cannot figure it out for the life of me. If I could get the images on the cards to be random then the game would be working fine. This is the only problem I seem to be having with it. Please let me know if you can help out, thank you!
You've not posted the one script that could be the problem. Everything to do with the randomization comes from this line
string type = GameSettings.Instance ().GetRandomType ();
So how does GetRandomType() work, what does it return?
Ah, I was not aware of that, sorry. This is the script that GetRandomType() pulls from. There are different cards in each difficulty setting. I just need to get them to show up on the 'top' part of my quad.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameSettings
{
private static GameSettings gameSettings;
private GameSettings(){}
public static GameSettings Instance()
{
if(gameSettings == null)
{
gameSettings = new GameSettings();
}
return gameSettings;
}
public enum GameDifficulty {Easy,$$anonymous$$edium,Hard}
private GameDifficulty difficulty = GameDifficulty.Easy;
private string[]easyDifficulty = {"TotCard1","OttoCard1","ClaraCard1"};
private string[]mediumDifficulty = {"TotCard1","OttoCard1","ClaraCard1","GabeCard1","$$anonymous$$imiCard1"};
private string[]hardDifficulty = {"TotCard1","OttoCard1","ClaraCard1","GabeCard1","$$anonymous$$imiCard1","LeoCard1","RoxyCard1"};
public List<string>CardTypes
{
get
{
List<string>tempList= new List<string>();
switch(difficulty)
{
case GameDifficulty.Easy:
tempList.AddRange(easyDifficulty);
break;
case GameDifficulty.$$anonymous$$edium:
tempList.AddRange(mediumDifficulty);
break;
case GameDifficulty.Hard:
tempList.AddRange(hardDifficulty);
break;
}
return tempList;
}
}
public void SetDifficulty(GameDifficulty diff)
{
difficulty = diff;
}
public string GetRandomType()
{
return CardTypes [Random.Range (0, CardTypes.Count)];
}
}
Your answer
Follow this Question
Related Questions
Instantiate a random prefab at an objects location 3 Answers
Instantiating a random prefab from array. 1 Answer
Is it possible to have a prefab of a cube, which has instances with different colours? 2 Answers
GetComponentInChildren not Working 1 Answer
Spawning - How to only have one object at a spawn at a time? 0 Answers