Advertisement
S-Hend

SJH Task 3 2025 - 3D Colour Changing Object

May 22nd, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ColorChange : MonoBehaviour
  7. {
  8.     [SerializeField]
  9.     private GameObject cube;
  10.  
  11.     private Renderer cubeRenderer;
  12.  
  13.     private Color newCubeColor;
  14.  
  15.     private float randomChannelOne, randomChannelTwo, randomChannelThree;
  16.  
  17.     void Start()
  18.     {
  19.         cubeRenderer = cube.GetComponent<Renderer>();
  20.         gameObject.GetComponent<Button>().onClick.AddListener(ChangeCubeColor);
  21.     }
  22.  
  23.     private void ChangeCubeColor()
  24.     {
  25.         randomChannelOne = Random.Range(0f, 1f);
  26.         randomChannelTwo = Random.Range(0f, 1f);
  27.         randomChannelThree = Random.Range(0f, 1f);
  28.  
  29.         newCubeColor = new Color(randomChannelOne, randomChannelTwo, randomChannelThree, 1f);
  30.  
  31.         cubeRenderer.material.SetColor("_Color", newCubeColor);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement