0
\$\begingroup\$

I made two textures for my 3D game character. I can use both of them by dragging them on my player but I want to do the same through code. I searched and found that people are either using:

1) rend.material.mainTexture = texts [0];

or

2) transform.GetComponent<Renderer>().material.mainTexture = texts [0];

but none of them works for me. I have a mesh renderer attached to my player. I set my code to swap the texture on my player when it reaches a certain amount of points. To debug I attached an empty material to my player and during run time so that I could see the texture on it but the swap didn't take place. The first texture is in texts[0] and the second one is in texts[1].

This is my code:

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

public class swaptexture : MonoBehaviour {

    public Texture[] texts;
    public gemlife g;

    // Use this for initialization
    void Start () {

        transform.GetComponent<Renderer> ().material.mainTexture = texts [0];
    }

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

        if (g.ratio >= 0.96f)
            transform.GetComponent<Renderer> ().material.mainTexture = texts [1];
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Your code looks like it should work (though be wary of leaving unwanted material copies floating around). Can you show us what your "empty material" looks like in the Inspector? Also, have you verified that the condition g.ration >= 0.96f is met? \$\endgroup\$
    – DMGregory
    Commented Dec 26, 2016 at 18:40
  • \$\begingroup\$ Have you tried GetComponent<MeshRenderer>() instead? \$\endgroup\$ Commented Dec 26, 2016 at 18:52
  • \$\begingroup\$ @DMGregory yes the condition is being met. before running: s28.postimg.org/wclyxa7f1/Untitled.png after running: s28.postimg.org/rf8ec65fx/Untitledsrgrsg.png \$\endgroup\$ Commented Dec 26, 2016 at 18:59
  • \$\begingroup\$ @DMGregory ok now i set materials array size to 0 in the mesh renderer and that blank material is gone: s23.postimg.org/ttvj25hpn/agagag.png \$\endgroup\$ Commented Dec 26, 2016 at 19:07
  • \$\begingroup\$ @DMGregory so after debugging some more i found out that my code is working fine and i can see the material being changed in the inspector during runtime but still for some reason it doesnt reflect on my player at all. it works if i drag and drop it directly on player but doesnt work though code :/ \$\endgroup\$ Commented Dec 27, 2016 at 14:05

1 Answer 1

1
\$\begingroup\$

I think there is a problem with your g.ratio >= 0.96f condition. In my opinion the code is working fine, you might have to check if the condition is met or not.

public Texture[] tex;

float timer = 0;
// Use this for initialization
void Start () 
{
    transform.GetComponent<Renderer>().material.mainTexture = tex[0];
}

// Update is called once per frame
void Update () 
{
    if (timer >= 1)
    {
        transform.GetComponent<Renderer>().material.mainTexture = tex[1];
        //timer = 0;
    }

    timer += Time.deltaTime;
}

First TextureSecond Texture

\$\endgroup\$
1
  • \$\begingroup\$ just like your inspector my inspector also shows the change in texture when my condition is being met but it doesnt reflect on my character. your ball changed from yellow to red but my character remains the same...its so frustating! \$\endgroup\$ Commented Dec 28, 2016 at 15:28

You must log in to answer this question.

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