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 johntomasydstu · Apr 07, 2017 at 06:46 AM · unity 5collidersspritescollider 2dontrigger

How can I change sprites when player enters Collider??????

Hi all, I am trying to get an object to change sprites if the player is touching the object and pressing the "J" key. I have a an object called "Tree", which has 3 child objects, "treetop01", "treelower01" and "collider", which has a BoxCollider2D trigger attached to it, as well as a script named "GatherResource". Im trying to get the "treetop01" object to go invisible and the "treelower01" object to change image after the player is touching the box collider using an OnTriggerStay2D in my "GatherResource" script thats attatched to the "collider" object, but I can't figure out how to change the image of the other objects. If anyone knows what I can do to change this please let me know, and if I'm not making sense or not being specific enough about anything please let me know so I can elaborate.

This is the tree object in the hierarchy: alt text

This is the "collider" object's inspector: alt text

This is the "GatherResource" script:

 using UnityEngine;
 using System.Collections;
  
 public class GatherResource : MonoBehaviour {
  
     void OnTriggerStay2D(Collider2D other)
     {
         if (Input.GetKeyDown("j"))
             Debug.Log("J key was pressed");
  
     }
      
 }

 

If there is any other information I need to provide just let me know. Thanks in advance!

screenshot-2017-04-05-204934.png (9.6 kB)
screenshot-2017-04-05-211049.png (52.5 kB)
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 UnityCoach · Apr 07, 2017 at 07:08 AM

You can either parse the hierarchy to find the child object by name and get its SpriteRenderer component, or simply add a SpriteRenderer reference and assign it manually.

 public class GatherResource : MonoBehaviour
 {
     public SpriteRenderer treeLower;
     public Sprite treeLowerGathered;
     
     void OnTriggerStay2D (Collider2D other)
     {
         if (other.tag == "Player" && Input.GetKeyDown("j"))
         {
             Debug.Log("J key was pressed");
             treeLower.sprite = treeLowerGathered;
         }
     }
  }
Comment
Add comment · 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
0

Answer by hirenkacha · Apr 07, 2017 at 07:19 AM

You can do it multiple ways.

  • Get a reference of objects which you need to modify in GatherResource script.

OR

  • Make a script for tree (parent) and have all references in that script including reference to GatherResource object. Have event Action in GatherResources and add listener into parent script and handle that to modify.

     using UnityEngine;
      using System.Collections;
       
      public class GatherResource : MonoBehaviour {
       
     public event Action OnKeyPressed;
          void OnTriggerStay2D(Collider2D other)
          {
              if (Input.GetKeyDown("j"))
              {
                  OnKeyPressed();
                  Debug.Log("J key was pressed");
              }
          }
      }
    
    
    

and on Parent Object class say Tree.cs

 public GameObject TreeTop;
 public SpriteRenderer TreeLower;
 public GatherResource ColliderScript;
 
 void Awake()
 {
           ColliderScript.OnKeyPressed += Gather;
 }
 
 void Gather()
 {
 }



Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I dynamically change colliders when animating a 2d sprite? 0 Answers

Generate Colliders not working on FBX imported object 1 Answer

How to change UI image with a name to a different image with a name 2 Answers

How to stop the carried object from moving way when parented to the player camera? 2 Answers

What are the best practices when importing 2D pixel sprites 1 Answer


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