1
\$\begingroup\$

I'm working on a game, in Unity3D, where the player picks up trash from Trash cans, and dumps the trash from the Trash cans into a Trash Truck. The world of the game itself is randomly generated, with randomly selected houses, with random rotations, and random Trees, Flowers, etc., etc.. I'm trying to make each one of the houses, have a randomly picked color, for each house. The problem is, I'm using a Shader Graph Material for the material of the houses, so how could I choose the random color, and for each house, not for the entire material, but just the material on each house? Also, I'm using a Vector 1 Node, plugged into the X Input Value of a Vector 2 Node, plugged into the Seed Input of a Random Range Node, which controls a Hue Node. I'm trying to control the value of the Vector 1 Node. Here's the Shader Graph Layout I have for this material: Shader Graph Node Layout Image And here's the current script I'm currently using to (try to,), control the color / Randomize the Color:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomHouse : MonoBehaviour
{
    public GameObject[] houses;
    private GameObject house;
    public float[] randRots = new float[] { 0.0f, 90.0f, 180.0f, 270.0f, 360.0f 
};

private List<GameObject> houseClone = new List<GameObject>();

public float timewait = 1.0f;
public RandomGen randGen;

private Renderer renderer;

// Start is called before the first frame update
void Start()
{
    RandHouse();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKey(KeyCode.T))
    {
        Debug.Log("Instantiate Update!");
        Destroy(house);
        RandHouse();
    }
}

void RandHouse()
{
    int randVal = Random.Range(0, randRots.Length);
    //MaterialPropertyBlock matBlock = new MaterialPropertyBlock();
    //matBlock.SetFloat("Vector1_172A5ED5", Random.Range(0.0f, 360.0f));
    house = Instantiate(houses[Random.Range(0, houses.Length)], new Vector3(0.0f, 0.0f, 0.0f), Quaternion.Euler(0.0f, randRots[randVal], 0.0f), transform);
    houseClone.Add(house);
    Renderer houserenderer = house.GetComponentInChildren<Renderer>();
    foreach (GameObject house in houseClone)
    {
        for (int mat = 0; mat < houserenderer.materials.Length; mat++)
        {
            Debug.Log("Current Material: " + houserenderer.materials[mat].name);
            if (houserenderer.materials[mat].name == "wall (Instance)")
            {
                Debug.Log("Mat Wall Found!");
                MaterialPropertyBlock matBlock = new MaterialPropertyBlock();
                matBlock.SetFloat("Vector1_172A5ED5", Random.Range(0.0f, 360.0f));
                renderer = house.GetComponentInChildren<Renderer>();
                renderer.SetPropertyBlock(matBlock, mat);
            }
        }
    }

    house.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
    //renderer.SetPropertyBlock(matBlock);
    StartCoroutine(WaitTime(timewait));
}

IEnumerator WaitTime(float wtime)
{
    Debug.Log("Start Wait Gen...");
    yield return new WaitForSeconds(wtime);
    Debug.Log("Wait Finished, Should be Genererating...");
    randGen.RandNew();
    yield return null;
}
}

I've currently commented-out the bits of code that are supposed to make the color random / randomize the color, since it seems to break another part of my script (I think it's actually the Coroutine, now that I think about it,),.

The way I've set this up is I have a Shader Graph Material, seen in the image above. The property name, in this case, for the RandomColor Vector1 Property I have, is Vector1_172A53D5.

I made a Plane GameObject, I also have two copies of my other Random GameObject Generating Script. (One for Trees, one for Flowers,),. As a child of this plane, I have an Empty GameObject, with this Random House Script on it. I have 5 different House Prefabs under the list of Houses. I have 5 different floats for the Random Rots (Rotations,), 0, 90, 180, 270, and 360. For the Timewait float, I have it set to 0.01, the time it waits before it Re-Generates the Plane's GameObjects, so that to make sure that they don't clip into any GameObject marked with the Tag, "NoSpawn". And for the Rand Gen script, I have it set to the Random GameObject Generating Script from the Parent, Plane GameObject. I have this script set up so that I can press the "T" Key During Runtime to Re-Randomize / Re-Randomly-Generate the Houses and Random GameObjects.

This other, Random GameObject Generating Script doesn't do anything to the House Script, all of the House-Generating-Related Things happen within the script I have here.

Also, here's a current example image of the Random-House-Generation Script, during Runtime. It currently only changes the colors of a few houses, I want all the houses to have a random color,:

An Example Image of the Partially-Working Random Color Generation'

I also have just realized, it appears as if only the First House Prefab (House_Prefab,), seems to get a Random Color, none of the 4 other prefabs seem to be able to get a Random Color, I don't know why.

If anyone needs any more information, please, let me know! Thanks!

\$\endgroup\$
15
  • 1
    \$\begingroup\$ Your material property block solution looks fine (though note that you don't need to allocate a new property block for every instance - the data gets copied on assignment so you can re-use the same block. Also, setting the material property directly may be more efficient if you're using SRP batching). Can you tell us what seems to be breaking when you do it this way? \$\endgroup\$ – DMGregory Dec 16 '20 at 15:07
  • \$\begingroup\$ What happens is, so I have another script, that generates random trees and flowers, etc., etc.. on the ground, on whichever, "plot" that the house is on. It Randomly Generates these GameObjects, and I have it so that they won't spawn inside any GameObject with a certain tag. It needs to be called in this Coroutine because otherwise, it won't not spawn in the specific tag, so it could clip into the house, trees, flowers, etc., etc.. When it breaks, I've noticed it seems to do this, it causes the Randomly Generated GameObjects to ignore the tag (The Tag, by the way, is called "NoSpawn",),. \$\endgroup\$ – Fox GAMING_NTF Dec 16 '20 at 15:17
  • 1
    \$\begingroup\$ This doesn't sound like it has much to do with changing colour. Want to ask about that problem instead? \$\endgroup\$ – DMGregory Dec 16 '20 at 15:18
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ – DMGregory Dec 16 '20 at 15:43
  • \$\begingroup\$ This is still an issue for me, any ideas? \$\endgroup\$ – Fox GAMING_NTF Dec 18 '20 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.