Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Katieb1650 · Jun 25, 2014 at 10:01 PM · textureprefabrandomrandomizequads

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jun 25, 2014 at 10:42 PM 0
Share

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?

avatar image Katieb1650 · Jun 26, 2014 at 03:10 PM 0
Share

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)];
     }
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges