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