- Home /
Changing the texture of another GameObject when pressing E
Hi all
I have created a collider that displays "press e" and I want to change the texture of a gameobject when I do so. My code works fine and prints the logs. The issue I am having is that the game object im am trying to change the texture from is not the one my script is attached to. I referenced my gameobject and made an array to try to change the material from the same script I set on the collider.
In my script I also created an array that lists the 2 textures I want to transition from. Note that I want to be able to switch back from one to the other with every click.
This is my script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class textirebutton : MonoBehaviour
{
public GameObject OpenPanel = null;
public GameObject hotl_exterior;
public Material[] material;
// Start is called before the first frame update
void Start()
{
//disable panel on start
OpenPanel.SetActive(false);
hotl_exterior = GetComponent<GameObject>();
}
//enable on trigger enter
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
OpenPanel.SetActive(true);
//Debug.Log("Entered");
}
}
void OnTriggerStay(Collider other)
{
if(Input.GetKeyDown(KeyCode.E) && other.tag == "Player")
{
Debug.Log("E pressed");
hotl_exterior.GetComponent<Renderer>().material = material[1];
}
}
//disable on trigger exit
void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
OpenPanel.SetActive(false);
//Debug.Log("Exit");
}
}
}
The error I get is :
MissingComponentException: There is no 'GameObject' attached to the "trigger" game object, but a script is trying to access it. You probably need to add a GameObject to the game object "trigger". Or your script needs to check if the component is attached before using it. UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/GameObject.bindings.cs:28) textirebutton.OnTriggerStay (UnityEngine.Collider other) (at Assets/textirebutton.cs:40)
Im sure the mistake is in that line where I try to access the material in the ontriggerstay function. I am not sure however, what I did wrong. Please someone help me :(
Apologies for the display there is so much text
Answer by Coffee-with-Venky · Mar 06, 2019 at 09:14 AM
hotl_exterior = GetComponent(); use this instead of ------- On Start() hotl_exterior = gameObject.this;
thanks for trying to help however I just tried this and it creates errors :/
Answer by mihoci10 · Mar 06, 2019 at 09:16 AM
When you are checking the other.tag == "Player" try doing other.gameobject.tag == "Player", because variable other is by type just a Collider which is a type of Component of a GameObject.
Thanks for the reply! I think I understand what you mean, but the "Player" tag is just used to trigger the actions, it is not the gameobject from which I want the texture changed.. Am I maybe missing your point?
Your answer
Follow this Question
Related Questions
Trigger not getting GameObject 2 Answers
GetComponent of copies of one gameobject? 0 Answers
GetComponent().Play(); Question(s) 2 Answers