I'm trying to automatically generate cubes in Unity with c#.
I have finally succeed in creating a large number of cubes (300x300), positioning them and naming them, but I have a problem when it comes to colouring them (last part of the code).
I want to colour them by using three variables, one for each RGB value. The variables I define in my code are rnewcolor
, gnewcolor
and bnewcolor
, and they are attributed to each component by using (last part of the code):
go.GetComponent<Renderer>().material.color = new Color(rnewcolor/255f, gnewcolor/255f, bnewcolor/255f)
When I attribute random values to these three variables for example:
int rnewcolor = UnityEngine.Random.Range(0, 255))
then the code works, and the cubes are coloured with random colours as expected (image here https://ibb.co/pXDmhfB )
But when I try to read the RGB values from a file, storing them on the arrangements Rcolor[i,j,k]
, Gcolor[i,j,k]
and Bcolor[i,j,k]
, to pass them later to the variables rnewcolor
, gnewcolor
, bnewcolor
, then the colour attribution does not work and I get all the cubes with a kind of grey colour which is not the colour read from the file (image https://ibb.co/S3JRnBr )
Some more images:
Inspector on the first cube (used for the instancing): https://ibb.co/VNsCy5b (it has the code "ProgramCubeNew.cs" added as a component) Inspector on the first cube (also in prefab): https://ibb.co/tKrVD2V
Some comments:
By printing some debugging, I know that the variables rnewcolor
, gnewcolor
, bnewcolor
, Rcolor[,,]
, Gcolor[,,]
and Bcolor[,,]
are all of the same type, which is Int32
.
Also, I know that the values read from the file and stored in the Rcolor[,,]
, Gcolor[,,]
and Bcolor[,,]
variables are right.
I know that here:
go.GetComponent<Renderer>().material.color = new Color(rnewcolor/255f, gnewcolor/255f, bnewcolor/255f)
the fucntion Color(,,)
works with float numbers ranging from 0
to 1
. It works perfectly when random numbers are assigned to the variables gnewcolor
, bnewcolor
and gnewcolor
but it does not work when values are read from the file.
As the colouring process work when using random numbers, i assume that the articulation between objects in the Unity interface must be right (?)
Does anybody knows why I can't attribute the colours when values are read from file?
Thank you!
using System.Collections;
using UnityEngine;
using System;
using System.IO;
using System.Text;
public class ProgramCubeNew : MonoBehaviour
{
public Int32[,,] Rcolor = new Int32[400, 400, 400];
public Int32[,,] Gcolor = new Int32[400, 400, 400];
public Int32[,,] Bcolor = new Int32[400, 400, 400];
// Start is called before the first frame update
void Start()
{
GameObject prefab = Resources.Load("Cube") as GameObject;
string path = @"H:\Unity\Learn_Everything_Fast\Cube_script_generation\export_bones_scene\rgb_bones_scene.txt";
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(' ');
int i = Convert.ToInt32(values[0]);
int j = Convert.ToInt32(values[1]);
int k = Convert.ToInt32(values[2]);
Rcolor[i, j, k] = Convert.ToInt32(values[3]);
Gcolor[i, j, k] = Convert.ToInt32(values[4]);
Bcolor[i, j, k] = Convert.ToInt32(values[5]);
}
}
// I'm using theese three lines in order to know if the file is being read ok, it seems so.
Debug.Log(Rcolor[50, 50, 50]);
Debug.Log(Gcolor[50, 50, 50]);
Debug.Log(Bcolor[50, 50, 50]);
for (int i = 0; i <= 300; i++)
{
for (int j = 0; j <= 300; j++)
{
int k = 50;
string name = string.Format("cube_{0}_{1}_{2}\n", i, j, k);
string namemat = string.Format("mat_{0}_{1}_{2}\n", i, j, k);
GameObject go = Instantiate(prefab) as GameObject;
go.transform.position = new Vector3(i, j + 20, k);
go.transform.name = name;
// int rnewcolor = UnityEngine.Random.Range(0, 255); // it works ok
// int gnewcolor = UnityEngine.Random.Range(0, 255); // it works ok
// int bnewcolor = UnityEngine.Random.Range(0, 255); // it works ok
int rnewcolor = Rcolor[i, j, k];
int gnewcolor = Gcolor[i, j, k];
int bnewcolor = Bcolor[i, j, k];
// I'm using this to print some debug
if (i == 50 && j == 50)
{
Debug.Log(Rcolor[i, j, k]);
Debug.Log(Gcolor[i, j, k]);
Debug.Log(Bcolor[i, j, k]);
Debug.Log(Rcolor[i, j, k].GetType());
Debug.Log(Gcolor[i, j, k].GetType());
Debug.Log(Bcolor[i, j, k].GetType());
Debug.Log(bnewcolor.GetType());
}
go.GetComponent<Renderer>().material.color = new Color(rnewcolor/255f,gnewcolor/255f,bnewcolor/255f);
}
}
}
// Update is called once per frame
void Update()
{
}
}