Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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 switching 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 achieve 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
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
Best Answer

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

var sprite1: Sprite;

gameObject.GetComponent(SpriteRenderer).sprite = sprite1;

Comment
Add comment · 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 : $$anonymous$$onoBehaviour
 {
 
     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
0

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 nothing (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
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Sprite Renderer changes visible position on being assigned to a sprite 1 Answer

Sprite Renderer is wrapping bottom pixel when it shouldn't be 1 Answer

How to instantiate object as child of another element 1 Answer

Gui.Box - Sprite 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 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