• 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
Question by FluzzyWuzzy · Jul 27, 2014 at 02:17 PM · texturespritespriterendererdoor

Script to change SpriteRenderers sprite field

I am creating a simple 2D door, when i stand in my trigger, i want it to open by switc$$anonymous$$ng texture from the open door texture to the closed one. I got it working with an animator where i had the transition between the textures occur when i entered and left the trigger area, but i wanted too see if it's possible to ac$$anonymous$$eve the same result with script only. I do not want to do it with 2 GameObjects however, i got that working too by toggling the enable on the spriteRenderer.

Is there anyone who can help me? I'm pretty new to coding, but I have searched everywhere and not found a single solution that matches what i want.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Fabulous-Design · Jul 27, 2014 at 02:22 PM

var sprite1: Sprite;

gameObject.GetComponent(SpriteRenderer).sprite = sprite1;

Comment

People who like this

0 Show 3 · 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 FluzzyWuzzy · Jul 27, 2014 at 02:38 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class openDoorCode : MonoBehaviour
 {
 
     SpriteRenderer renderer;
     Sprite Open;
     Sprite Closed;
 
     void Start()
     {
         Open = Resources.Load<Sprite>("Door_Open");
         Closed = Resources.Load<Sprite>("Door_Closed");
         renderer = GetComponent<SpriteRenderer>();
     }
 
     void FixedUpdate()
     {
     }
 
     void OnTriggerEnter2D(Collider2D Collider)
     {
         renderer.sprite = Open;
         print("Open");
     }
     void OnTriggerExit2D(Collider2D Collider)
     {
         renderer.sprite = Closed;
         print("Close");
     }
 
 }

This is my code so far, as of now, both when i enter and exit the area, it sets the renderer.sprite to none. What am I missing? And thank you for your help, much appreciated :)

avatar image Exbow · Jul 27, 2014 at 02:39 PM 0
Share

and in C#

 Sprite sprite1;
 gameObject.GetComponent<SpriteRenderer>().sprite = sprite1;



avatar image FluzzyWuzzy · Jul 27, 2014 at 05:54 PM 0
Share

Well, I couldn't get the Resources.Load to work on my own so i made the Sprite public and assigned it in the inspector. Works like a charm ;)!

     SpriteRenderer render;
     public Sprite Open;
     public Sprite Closed;
 
     void Start()
     {
         render = GetComponent<SpriteRenderer>();
     }
 
     void FixedUpdate()
     {
     }
     
     void OnTriggerEnter2D(Collider2D Collider)
     {
         render.sprite = Open;
     }
     
     void OnTriggerExit2D(Collider2D Collider)
     {
         render.sprite = Closed;
     }
 
 }
avatar image

Answer by TallBrian · Mar 05, 2017 at 03:27 PM

The resources need to be in a folder named "Resources" in your project. PNG files in the Assets folder are not found. I was getting not$$anonymous$$ng (like you) until I made a folder called "Resources" put copies of my PNGs in then. Then it worked like great. see: "Loading Resources at Runtime"

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help getting texture scale right 0 Answers

Change sprite texture through script 4 Answers

Gui Box - Texture - Spritesheet 0 Answers

How change sprite animation texture?? 2 Answers

How to scroll a sprite? 0 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