-2
\$\begingroup\$

I am trying to export my Unity project to Android Studio so that I can create some Android buttons that "controls" the Unity app's behavior. I know what I have to do in order to let it access through Java, but I don't know how to change the color. I watched a video about on Youtube, and the guy used: gameObject.renderer.material.color. However, when I try to do the same it can't find material.

This is my script so far:

using UnityEngine;
using System.Collections;

public class ChangeColor : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

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

    }

    void ReceiveRotDir(string message)
    {
        if (string.Equals(message, "G"))
        {
            this.gameObject.renderer.m ///It can't find material
        }
        else if (string.Equals(message, "P"))
        {

        }
        else if (string.Equals(message, "B"))
        {

        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ @JohnHamilton, we do not accept duplicates across sites. Your links are for Stack Overflow, not GameDev. \$\endgroup\$ – Gnemlock Dec 13 '16 at 21:37
  • 1
    \$\begingroup\$ @Gnemlock still it shows lack of research. Just looking at the docs (not even requires any thinking) is enough to solve this. \$\endgroup\$ – John Hamilton Dec 14 '16 at 5:39
0
\$\begingroup\$

Instead of referencing "renderer" you need to actually reference the renderer component you are targeting.

For example:

gameObject.GetComponent<MeshRenderer>().material.color
\$\endgroup\$
  • \$\begingroup\$ so like this? gameObject.GetComponent<MeshRenderer>().material.color(Color.black); \$\endgroup\$ – user4118143 Dec 13 '16 at 13:12
  • \$\begingroup\$ No, that would not compile. Color is not a function it is a property. \$\endgroup\$ – jgallant Dec 13 '16 at 13:14
  • \$\begingroup\$ gameObject.GetComponent<MeshRenderer>().material.color = Color‌​.black; \$\endgroup\$ – user4118143 Dec 13 '16 at 13:16
  • \$\begingroup\$ Yep, you got it. Make sure you are targeting the proper renderer type for your gameobject. \$\endgroup\$ – jgallant Dec 13 '16 at 13:17
  • \$\begingroup\$ How do I do that I am targeting the proper renderer type? The gameobject has a mesh renderer \$\endgroup\$ – user4118143 Dec 13 '16 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.