Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 farayman3 · Apr 13, 2015 at 06:37 AM · spritelistspriterendererspecific

How to use spriteRenderer.sprite for a GameObject in a list?

I have a grid containing sprites and once I click one I have it change its sprite. However I would simultaneously want to change the sprite next to it. The way I thought to do this was by creating a list of the instantiated objects so I can access them later.

     public void OnMouseDown()
         {
             ChangeTheSprite ();
         }
     public void ChangeTheSprite(){
 
         itemlist = GetComponent<BoardManager> ();
 
         if (spriteRenderer.sprite == redLight) {
 
             spriteRenderer.sprite = greenLight;
 
             // Here I try to change another sprite
             itemlist.redLightList[1].GetComponent<SpriteRenderer> ().sprite = greenLight;
             }
         else {
             spriteRenderer.sprite = redLight;
             }
         }

(for the example I'm simply changing the sprite on the 2nd grid location, the "next to it" will come later, though I suspect that it might become a problem as well as I don't know how to get the position of an object obtained with OnMouseDown in a list)

What am I doing wrong here?

Comment
Add comment · Show 3
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 apple92 · Apr 13, 2015 at 01:46 PM 0
Share

Edit: huh.. Overread the sentence at the bottom of your Question :)

You could add a simple, clean script to each Object with only two public int variables for the row number and the number within a row (All sprites of one row are it's childs). Then while initializing the whole system (See below) you'd adjust the number to match the position inside the row. So you'd run a counter while iterating (foreach) over the childs of a row, keeping the counter the same number as the position within the list.

When you click, you only need to access the script, read the both int and there is your position.

Initialization:

Generate a list for each row and then create a parent list that contains all rows.

Like this you could first access the row and then the position of the sprite. If you now want to change the sprite beside, you'd only need increment/decrement the row number by 1

(Pseudo $$anonymous$$atrix style :) )

 Row 1: Seat1, Seat2, Seat3, Seat4
 Row 2: Seat1, Seat2, Seat3, Seat4
 Row 3: Seat1, Seat2, Seat3, Seat4
 Row 4: Seat1, Seat2, Seat3, Seat4
 
 List[0][0] is Seat 1 in row 1
 List[1][0] is Seat 1 in row 2
 List[1][4] is Seat 4 in row 2

and so on...

Should give you an idea on how to do it. Didn't test it though :)

avatar image apple92 · May 29, 2015 at 08:47 PM 0
Share

Problem solved or did you give up? ;)

avatar image farayman3 · May 29, 2015 at 08:53 PM 0
Share

Yes. I ended up starting from scratch and have a bool for each item. All the items are placed in a array so I can easily access the right one. I even have my alpha of the game now but do not really have the time to continue. Thanks for your interest however!

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Sprite Mask Render Issue 1 Answer

Why do my GameObjects have a flickering line above them? 2 Answers

My gameObject is invisible 0 Answers

How to access SpriteRenderer Color from a shader? 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