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 /
  • Help Room /
avatar image
0
Question by C_Corporatiion · Mar 18, 2021 at 03:48 PM · randomplayerprefsmaterials

PlayerPrefs for Random Materials

Okay, so below there will be a block of text, and I'm sorry, but hear me out: There's one scene which generates the "world" using a 2d array. Then, the next scene is the game itself, which is using these values to create colors for materials. According to the Debug Logs i had, the Material.SetColor() part is where it breaks. The material is completely white, not even reacting to shadows, but when i click the material in the inspector, it is the color that it's supposed to be. I tried changing the EmissiveColor_ but the effect stays the same. Here's the code.

worldGenerator

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class worldGenerator : MonoBehaviour
 {
 //46 46 100
 public int[] matr = new int[10];
 public int[] matg = new int[10];
 public int[] matb = new int[10];
 public int[] mathrd = new int[10];
 public int[] matrct = new int[10];
 public string[] matt = new string[10];
 public int[,] world = new int[100,100];
 public int[] matx = new int[10];
 public int[] maty = new int[10];
 
 // Start is called before the first frame update
 void Start()
 {
     for (int i = 0; i < 5; i++)
     {
         matr[i] = Random.Range(0, 101);
         matg[i] = Random.Range(0, 101);
         matb[i] = Random.Range(0, 101);
         mathrd[i] = Random.Range(1, 6);
         matrct[i] = 0;
         matt[i] = "wood";
         matx[i] = Random.Range(0, 101);
         maty[i] = Random.Range(0, 101);
     }
 
     for (int i = 0; i < 5; i++)
     {
         matr[i + 5] = Random.Range(0, 101);
         matg[i + 5] = Random.Range(0, 101);
         matb[i + 5] = Random.Range(0, 101);
         mathrd[i + 5] = Random.Range(3, 11);
         matrct[i + 5] = 1;
         matt[i + 5] = "rock";
         matx[i + 5] = Random.Range(0, 101);
         maty[i + 5] = Random.Range(0, 101);
     }
 
     for (int i = 0; i < 100000; i++)
     {
         int j = 0;
         foreach (int item in matx)
         {
             world[matx[j], maty[j]] = j;
             matx[j] += Random.Range(-1, 2);
             if (matx[j] >= 100 || matx[j] <= -1)
             {
                 matx[j] = 100 - Mathf.Abs(matx[j]);
             }
             maty[j] += Random.Range(-1, 2);
             if (maty[j] >= 100 || maty[j] <= -1)
             {
                 maty[j] = 100 - Mathf.Abs(maty[j]);
             }
             j++;
         }
     }
 
     for (int x = 0; x < 100; x++)
     {
         for (int y = 0; y < 100; y++)
         {
             PlayerPrefs.SetInt(x.ToString() + "w" + y.ToString(), world[x, y]);
         }
     }
 
     for (int i = 0; i < 10; i++)
     {
         //PlayerPrefs.SetInt("mh" + i.ToString(), math[i]);
         //PlayerPrefs.SetInt("ms" + i.ToString(), mats[i]);
         //PlayerPrefs.SetInt("mv" + i.ToString(), matv[i]); 
         PlayerPrefs.SetInt("mr" + i.ToString(), Mathf.RoundToInt(matr[i] * 100));
         PlayerPrefs.SetInt("mg" + i.ToString(), Mathf.RoundToInt(matg[i] * 100));
         PlayerPrefs.SetInt("mb" + i.ToString(), Mathf.RoundToInt(matb[i] * 100));
         PlayerPrefs.SetInt("mhrd" + i.ToString(), mathrd[i]);
         PlayerPrefs.SetInt("mrct" + i.ToString(), matrct[i]);
         PlayerPrefs.SetString("mt" + i.ToString(), matt[i]);
     }
 
     PlayerPrefs.SetInt("px", Random.Range(0, 100));
     PlayerPrefs.SetInt("py", Random.Range(0, 100));
 }
 }

gameManager

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class gameManager : MonoBehaviour
 {
     public GameObject rockTemplate;
     public GameObject treeTemplate;
     GameObject o;
     Vector2 placeat = new Vector2(0, 0);
     Color materialColor;
 
 // Start
 void Start()
 {
     int px = PlayerPrefs.GetInt("px", 0);
     int py = PlayerPrefs.GetInt("py", 0);
 
     int dm = PlayerPrefs.GetInt(px.ToString() + "w" + py.ToString(), 0);
 
     int mr = PlayerPrefs.GetInt("mr" + dm.ToString(), 0);
     int mg = PlayerPrefs.GetInt("mg" + dm.ToString(), 0);
     int mb = PlayerPrefs.GetInt("mb" + dm.ToString(), 0);
     
     int mhrd = PlayerPrefs.GetInt("mhrd" + dm.ToString(), 0);
     int mrct = PlayerPrefs.GetInt("mrct" + dm.ToString(), 0);
     string mt = PlayerPrefs.GetString("mt" + dm.ToString(), "");
 
     //I assure you, this is the most problematic thing in the whole game
     materialColor = new Color(mr / 100, mg / 100, mb / 100);
 
     switch (mt)
     {
         case "wood":
             o = treeTemplate;
             break;
         case "rock":
             o = rockTemplate;
             break;
         default:
             break;
     }
 
     for (int i = 0; i < 256; i++)
     {
         placeat = new Vector2(Random.Range(-256, 257), Random.Range(-256, 257));
         GameObject to = Instantiate(o, new Vector3(placeat.x, 0.5f, placeat.y), Quaternion.identity);
         to.GetComponent<Renderer>().sharedMaterial.SetColor("_Color", materialColor);
     }
 }
 }
Comment
Add comment
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

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

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to randomly instantiate prefabs but each with it's own probability? 2 Answers

random question without repetition 0 Answers

Can someone please help me find a logic flaw in my code? 0 Answers

Using PlayerPrefs to remember transform heirarchies? 0 Answers

Why my LevelSelect Dont unlock 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