-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\$
2
  • \$\begingroup\$ @JohnHamilton, we do not accept duplicates across sites. Your links are for Stack Overflow, not GameDev. \$\endgroup\$
    – Gnemlock
    Commented Dec 13, 2016 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\$ Commented Dec 14, 2016 at 5:39

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.