- Home /
Change Material of quad in script c# unity 2d
I am trying to make an object select a specific texture / Mesh gameobject. Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SeasonSwitch : MonoBehaviour
{
public Material Fruh;
public Material Zom;
public Material Ehr;
public Material Shi;
public GameObject Object;
private Text seasonText;
void Start()
{
seasonText = GameObject.Find("Season").GetComponent<Text>();
}
void Update()
{
SelectMat();
}
void SelectMat()
{
if (seasonText == Fruh)
{
Object.GetComponent<MeshRenderer>().material = Fruh;
}
else if (seasonText == Zom)
{
Object.GetComponent<MeshRenderer>().material = Zom;
}
else if (seasonText == Ehr)
{
Object.GetComponent<MeshRenderer>().material = Ehr;
}
else if (seasonText == Shi)
{
Object.GetComponent<MeshRenderer>().material = Shi;
}
}
}
Am I missing a component in here?
I have also tried the following script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SeasonSwitch : MonoBehaviour
{
public Material Fruh;
public Material Zom;
public Material Ehr;
public Material Shi;
public Renderer Object;
private Text seasonText;
void Start()
{
seasonText = GameObject.Find("Season").GetComponent<Text>();
}
void Update()
{
SelectMat();
}
void SelectMat()
{
if (seasonText == Fruh)
{
Object.material = Fruh;
}
else if (seasonText == Zom)
{
Object.material = Zom;
}
else if (seasonText == Ehr)
{
Object.material = Ehr;
}
else if (seasonText == Shi)
{
Object.material = Shi;
}
}
}
Please, and thank you!
Comment
Answer by Zaeran · May 19, 2019 at 08:05 PM
seasonText is a Text component, but you're trying to compare it to a Material.
I'm guessing you want to compare the value of the text string to the name of the material? in which case, you need to do this:
if(seasonText.text == "Fruh"){
Object.material = Fruh;
}
I'd also consider changing the variable name Object to Obj, as Object shares the name with a type.
That worked. Thank you! Now the system is working properly, and It applies to the setup the way I was intending.
I have changed object to Tile so the project does not bug up