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 many_questions · Nov 12, 2014 at 06:41 PM · transformspritesizelocalscale

Modify Sprite size to reflect parent element

I am trying to randomly place a sprite inside a another square sprite inside a gameObject.

I have the random part working, however I cannot seem to modify the size of the randomly selected sprites. Some are within the bounds of the parent sprite, some are not. My goal is modify the scale so as to have the sprites that exceed the size of the square sprite shrink so that they fit inside the square sprite.

I was looking at transform.localScale because it says it is transformed relative to the parent.

Anyway, here is what I have so far. I commented out my attempt at transform.localscale because it didn't seem to do anything useful.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 /************************************************************************
 * Class used to randomly show shape sprites in draw area
 * 
 *
 *
 ************************************************************************/
 
 public class show_shape_MD : MonoBehaviour {
     //providing slots in script for available shape sprites
     public Sprite sprite1; 
     public Sprite sprite2;
     public Sprite sprite3;
     public Sprite sprite4;
     public Sprite sprite5;
     public Sprite sprite6;
     public Sprite sprite7;
     public Sprite sprite8;
     public Sprite sprite9; 
     public Sprite sprite10;
     public Sprite sprite11;
     public Sprite sprite12;
     public Sprite sprite13;
     public Sprite sprite14;
     public Sprite sprite15;
     public Sprite sprite16;
         
     public float width = 500;
     public float height = 500;
 
     private SpriteRenderer spriteRenderer; 
 
     void Start ()
     {
         spriteRenderer = GetComponent<SpriteRenderer>();
         if (spriteRenderer.sprite == null)
             spriteRenderer.sprite = sprite1;
     }
     //On mouse down change sprite randomly
     void Update (){
             if (Input.GetMouseButtonDown(0))
         {
             //call to randomly change sprite
             random_Sprite_change ();
         }
     }
     //function to randomly select a sprite
     void random_Sprite_change (){
         Debug.Log ("stepped into sprite change function");
         int randNum = Random.Range (1, 16);
         switch(randNum){
             case 1: spriteRenderer.sprite = sprite1; break;
             case 2: spriteRenderer.sprite = sprite2; break;    
             case 3: spriteRenderer.sprite = sprite3; break;
             case 4: spriteRenderer.sprite = sprite4; break;    
             case 5: spriteRenderer.sprite = sprite5; break;
             case 6: spriteRenderer.sprite = sprite6; break;    
             case 7: spriteRenderer.sprite = sprite7; break;
             case 8: spriteRenderer.sprite = sprite8; break;
             case 9: spriteRenderer.sprite = sprite9; break;
             case 10: spriteRenderer.sprite = sprite10; break;
             case 11: spriteRenderer.sprite = sprite11; break;
             case 12: spriteRenderer.sprite = sprite12; break;    
             case 13: spriteRenderer.sprite = sprite13; break;
             case 14: spriteRenderer.sprite = sprite14; break;    
             case 15: spriteRenderer.sprite = sprite15; break;
             case 16: spriteRenderer.sprite = sprite16; break;
             }
         sprite_scale ();
     }
     //used to scale sprite based on parent sprite size
     void sprite_scale (){
         /*Vector3 scale = new Vector3( width, height, 1f );
         transform.localScale = scale;
         transform.localScale.Equals(spriteRenderer.sprite);*/
         }
 }


Please note my scales are not to the size of the parent sprite, I was just making sure I could notice if anything changed.

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 flowery · Mar 05, 2016 at 02:53 PM 0
Share

Hey, have you found a solution? I'm having the same issue here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Mar 06, 2016 at 04:46 AM

first i prefer using an array to manage all the sprites

 public Sprite[] sprites;
 
 spriteRenderer.sprite = sprites[Random.Range(0,sprites.Length)];

To make it fix in a square, you can use the data from sprite.bounds

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can't get Object size to Reset to Original 1 Answer

What is the reasoning of multiplying by Time.deltaTime 1 Answer

Does 2D sprite (art asset) size matters ? 1 Answer

How can I get Image size? 0 Answers

How to make an object's localscale values grow and shrink within a few seconds? 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