Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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 Shablo5 · Jul 13, 2015 at 12:51 AM · 2dcollisionspritespritesheet

Changing sprites from spritesheet on collision. What am I doing wrong?

My code is listed below in a pastebin link. I've tried my best, but I just can't seem to do it.

I'm trying to get a block to switch to a different sprite every time it is collided with, and finally be destroyed. I just... Can't seem to get it working when I have switched to a sprite sheet. Help please :)

http://pastebin.com/HBST56vZ

Comment
Add comment · Show 1
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 Aridez · Jul 13, 2015 at 01:14 AM 0
Share

I have not worked with sprites too much but i'm sure that following these steps you will be able to debug your code:

  1. $$anonymous$$ake sure that the "OnCollisionEnter2D(Collision2D col)" function is called, maybe something on the colliders was not set up correctly. You can do that by simply calling "debug.log" inside that function and look if it is printed correctly.

  2. If the function is called then the next problem might be the line "if (this.gameObject.GetComponent().sprite == redblock)", make sure that this line is working correctly (to me it seems a bit odd) if not look up some documentation about sprites, you could start here and it should not take you too long to solve (or you could just google it). You can check that placing the debug.log inside the if statement.

  3. In case everything else works the mistake must be in the line "this.gameObject.GetComponent().sprite = yellowblock;" and again, look up the unity documentation about sprites and how to assign them.

Good luck and let me know if that helps!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JoJoJet · Jul 13, 2015 at 11:11 AM

I completely re-wrote your script, making it less complicated and fixing your problem:

 using UnityEngine;
 using System.Collections;
 
 public class block : MonoBehaviour {
     public Sprite[] blockColors; //Assign the length of the array in the inspector,
     //and drag the sprites you want it to change to into each element.
     public int currentSprite = -1; //The int that tells us which sprite in the array
     //we will use.
     public bool deleting; //Unity doesn't actually delete an object until the next
     //frame, so this will allow us to make sure the code doesn't break by having
     //currentSprite be longer than the array length.
     private int arrayLength;
 
     void Start() {
         arrayLength = blockColors.Length - 1;
     }
 
     void OnCollisionEnter2D() {
             currentSprite += 1; // adds 1 to the "currentSprite" integer.
 
         if (currentSprite > arrayLength) { //Deletes the object if currentSprite is greater than the
             //length of the blockColors array
             deleting = true;
             GameObject.Destroy (gameObject);
         }
         if (!deleting) {
             GetComponent<SpriteRenderer> ().sprite = blockColors [currentSprite]; //sets the SpriteRenderer's
             //Sprite to be the desired sprite in the blockColors array.
         }
     }
 }
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

Two different sprites animation running simultaneously 0 Answers

Push only 1 box around at a time (2D) 1 Answer

Sprites in spritesheet have strange edges in the sprite editor but not in art software 2 Answers

Change entire spriteSheet from animation tree 2 Answers

how do I change a 2D Sprite Character Image/Graphic to another Sprite Character Image/Graphic with the same movements, animation,mechanim etc. 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