1
\$\begingroup\$

So I want to load different textures for my game object when different conditions are met. There are no errors when I compile this code. The colours change according to the condition, that part does work. The textures do not load though. The textures are stores in a Resources folder in Unity. The first code works but the second does not. Why is this the case? Below is my code:

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

public class Level4 : MonoBehaviour
{

    Material _mat;
    GameObject Sphere;


    // Use this for initialization
    void Start()
    {
        Sphere = GameObject.FindWithTag("Player");
        //Material _mat = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;

    }

    // Update is called once per frame
    void Update()
    {
        try
        {


            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                Renderer _rend = GetComponent<Renderer>();
                Material _mat = _rend.material;
                _rend.material.mainTexture = Resources.Load("face1") as Texture;
                _mat.color = Color.red;
            }
        }
        catch (System.Exception)
        {
        }
    }
}

this is the second code that does not work:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class arduino : MonoBehaviour
{

    SerialPort sp = new SerialPort("//.////COM6", 9600);// Com port and the baud rate of the arduino
    Material m_Material;
    GameObject Sphere;



    void Awake()
    {
        Sphere = GameObject.FindWithTag("Player");
        m_Material = GameObject.FindWithTag("Player").GetComponent<Renderer>().material;
    }
    void Start()
    {
        if (!sp.IsOpen)
        { // If the erial port is not open 
            sp.Open(); // Open 
        }
        sp.ReadTimeout = 250; // Timeout for reading 
    }

    // Update is called once per frame
    void Update()
    {
        if (sp.IsOpen)
        { // Check to see if the serial port is open 
            try
            {

                string portreading = sp.ReadLine(); // get the string output of the serial port 
                float amount = int.Parse(portreading);



               if ((amount > 251f))
                {

                    Renderer _rend = GetComponent<Renderer>();
                    m_Material = _rend.material;
                    _rend.material.mainTexture = Resources.Load("face1") as Texture;
                    m_Material.color = Color.red;

                }
            }
            catch (System.Exception)
            {

            }

        }

    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Thanks for all the answers and I will try to answer all the questions here. I have loaded the full code here and did not load it earlier as I did not want people to have to read through the entire code and waste time. With regards to errors, I am not receiving any errors. The colours do change as the conditions change but the textures do not load at all. I have placed the textures in a Resources folder. \$\endgroup\$
    – user19964
    Commented Jul 27, 2018 at 1:35
  • \$\begingroup\$ I notice you've wrapped all your code in a big try-catch with an empty catch block, something generally considered risky practice. This might be masking relevant error messages from showing up in your console. Can you check to see if there's any exception being caught and what it says? \$\endgroup\$
    – DMGregory
    Commented Jul 27, 2018 at 14:07
  • \$\begingroup\$ There are no exceptions being caught as far as I can tell \$\endgroup\$
    – user19964
    Commented Aug 10, 2018 at 4:22

1 Answer 1

1
\$\begingroup\$

Take a look at the documentation for examples. Renderer.material

also make sure you have placed the texture in a folder named Resources.

you can reference the Renderer then get a reference to the material and then load it and change the color.

if ((amount > 151f) && (amount < 200f)) 
{
    Renderer _rend = GetComponent<Renderer>();
    Material _mat = _rend.material;

    //make sure face2 is in Assets/Resources 
    _rend.material.mainTexture = Resources.Load("face2") as Texture;
    _mat.color = Color.blue;
}
\$\endgroup\$
4
  • \$\begingroup\$ Happy to help. Mark as the answer if this works for you. \$\endgroup\$ Commented Jul 27, 2018 at 12:59
  • \$\begingroup\$ so this code worked for one code but not the other. I have updated the question accordingly. I cannot see why it will not work for the second code. \$\endgroup\$
    – user19964
    Commented Aug 10, 2018 at 4:18
  • 1
    \$\begingroup\$ I would change the m_material to Sphere.GetComponent<Renderer>().material another is the timeout is 250 so can it ever reach greater than 250? and below you re-assign the mat I would use the object again so try Renderer _rend = Sphere.GetComponent<Renderer>(); and it should load fine again \$\endgroup\$ Commented Aug 10, 2018 at 14:57
  • \$\begingroup\$ Thank you once again, it works like a charm :) really appreciate all the help Justin \$\endgroup\$
    – user19964
    Commented Aug 12, 2018 at 1:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .