Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dfhds · Mar 06, 2019 at 08:31 AM · gameobjecttexturetriggergetcomponent

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dfhds · Mar 06, 2019 at 09:40 AM 0
Share

thanks for trying to help however I just tried this and it creates errors :/

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dfhds · Mar 06, 2019 at 09:38 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

185 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger not getting GameObject 2 Answers

GetComponent of copies of one gameobject? 0 Answers

Is there an event/hook/trigger to sub to when a texture is applied to a gameObject in scene in editor mode? 1 Answer

BCE0019: 'cont' is not a memBCE0019: 'cont' is not a member of 'UnityEngine.Component'. ber of 'UnityEngine.Component'. 1 Answer

GetComponent().Play(); Question(s) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges