Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity change material

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

public class DeerScript : MonoBehaviour
{
	// this is gameobject whose material will be changed
    public GameObject target;
    
    // this is array of materials
    // which is assigned in inspector
    public Material[] materials;
    
    // in this example i have used trigger
    private void OnTriggerEnter(Collider other)
    {
    	// this the type of mesh renderin used in gameobject
        // your may be different  like MeshRenderer
        // you can find in inspector of target
        target.GetComponent<SkinnedMeshRenderer>().materials = materials;
    }
}
// here is demo of that example but it slightly differs from the above 
// instead of array pre assigned is have used aonther way around
// demo is in the link
// if this help please subscribe my youtube channel
// https://youtu.be/XY6XiLEh4II
Comment

unity change material

var newObstacle = Instantiate(cube, new Vector3(i, 0, j), Quaternion.identity, transform);
Renderer obstacleRenderer = newObstacle.GetComponent<Renderer>();

Material mat = new Material(obstacleRenderer.sharedMaterial);
mat.color = GetRandomColor();
obstacleRenderer.sharedMaterial = mat;
// for reference methods
public Color GetRandomColor()
{
  return new Color(Random.Range(0, 255),Random.Range(0, 255),Random.Range(0, 255));
}

// for specific
public Color GetRandomColor()
{
    var random = Random.Range(1, 10); 
    if(random<4) return Color.red; 
    else if(random<7) return Color.blue; 
    else return Color.green; 
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: reflection get enum value C# 
Csharp :: c# textbox only numbers 
Csharp :: c# switch example 
Csharp :: How can I use Hex color Unity? , give hex color in unity 
Csharp :: inheritance in c# 
Csharp :: get xml from url 
Csharp :: c# extension 
Csharp :: c# reflection create generic type 
Csharp :: asp.net web forms 
Csharp :: c# copy an object 
Csharp :: js if empty then 0 
Csharp :: c# convert datetime to timespan 
Csharp :: c# delete object 
Csharp :: calculate textbox value c# 
Csharp :: change tab to enter in c# form 
Csharp :: how to make a chunk loader in c# 
Csharp :: hive survive 
Csharp :: empty int array c# 
Csharp :: c# Class instance 
Csharp :: c# delay 1 second 
Csharp :: real world example of sinleton design pattern 
Csharp :: c# windows forms how to get controls in gropu box 
Csharp :: c# datafield change cell background color 
Csharp :: angular === vs == 
Csharp :: Custom Encrypted String Type 
Csharp :: how to get text color alpha unity 
Csharp :: how to use a round image unity 
Csharp :: last word of string to uppercase c# 
Csharp :: PasswordBox Helper 
Csharp :: unity check if animator has parameter 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =