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 coolbird22 · Mar 21, 2014 at 07:36 AM · instantiatescalelocalscalekey press

How to make an object scale indefinately on key press?

I'm using a code which doesn't seem to work for me. On Space press, I manage to instantiate the prefab but it does not scale for some reason. Thanks for your time :)

 using UnityEngine;
 using System.Collections;
 
 public class SpawnSmashRadius : MonoBehaviour {
 
     private GameObject smashRadius;
     float speed = 5f;
 
     
     void Update ()
     {
         if(Input.GetKey(KeyCode.Space))
         {
             GameObject radius;
             radius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
             radius.transform.parent = smashRadius.transform;
         }
         //Scale the object
                  Vector3 newScale = gameObject.transform.localScale;
                  gameObject.transform.localScale += gameObject.transform.localScale * Time.deltaTime * speed;
                  gameObject.transform.localScale = newScale;
 
         
 
     }
 }    
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 SirCrazyNugget · Mar 21, 2014 at 08:05 AM 0
Share

It's not easy to see what you're trying to do. After you've initialized the object you're getting the scale of the object which the script is attached to, not the one you've just spawned. Then you're storing this scale in a variable of newScale, perfor$$anonymous$$g some scaling on that object (which you'll never see) then setting the scale back to it's original scale.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by coolbird22 · Mar 22, 2014 at 09:19 PM

Bingo ! That is exactly what I needed. Such a simple fix to that ! Thanks a lot for all the help and suggestions. So, here is the final code that worked for me.

 void Update (){
         if(Input.GetKeyDown(KeyCode.Space))
         {
             cloneRadius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
             cloneRadius.transform.parent = smashRadius.transform;
         }
 
         //Scale the object
         if (Input.GetKey(KeyCode.Space)) {
             if (cloneRadius != null) {
 
                 cloneRadius.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);
 
             }
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
avatar image
0

Answer by CodeElemental · Mar 21, 2014 at 08:07 AM

It is due to your code negating the changes already made.

 Vector3 newScale = gameObject.transform.localScale; // You set the newscale to be the original unmodified scale
 gameObject.transform.localScale += gameObject.transform.localScale * Time.deltaTime * speed; // You increase the scale of the object with the Time.deltaTime factor
 gameObject.transform.localScale = newScale; // You set the scale to the original ummodified one , effectively negating the change you made to the previous code.

You should remove the last line (gameObject.transform.localScale = newScale;)in this code segment.

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
avatar image
0

Answer by robertbu · Mar 21, 2014 at 08:24 AM

Problem #1, you are using Input.GetKey() instead of Input.GetKeyDown(). Even for a brief press, you will get multiple frames with and therefore multiple Instantiated objects. Problem #2, you are scaling the game object with this script, not the game object you just created. And potential problem #3, 'smashRadius' is never initialized (at least not in the code you posted).

I'm not sure how you want this to work, but try this:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnSmashRadius : MonoBehaviour {
     
     private GameObject smashRadius;
     float speed = 5f;
     GameObject radius;
 
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             radius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
             //radius.transform.parent = smashRadius.transform;
         }
         //Scale the object
         if (radius != null) {
             Vector3 newScale = radius.transform.localScale;
             newScale += gameObject.transform.localScale * (Time.deltaTime * speed);
             radius.transform.localScale = newScale;
         }
     }
 }

And you didn't need to use the 'newScale' temporary variable. You only have to use a temporary if you are trying to change a single component (x,y,z) rather than the whole thing. You could do:

  radius.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);
Comment
Add comment · Show 2 · 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 coolbird22 · Mar 22, 2014 at 07:50 AM 0
Share

Ok, I changed around some code that was hindering me with regards to the Instantiating the prefab. On using the code you suggested, nothing really happens when I press Space. Here is the code

     void Update (){
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             cloneRadius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
             cloneRadius.transform.parent = smashRadius.transform;
         }
 
         //Scale the object
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
             if (cloneRadius != null) {
 
                 cloneRadius.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);
 
             }
 }
avatar image robertbu · Mar 22, 2014 at 01:25 PM 0
Share

This code is not my code. Did you try my code as posted? As for this code, if the goal is press and then hold space and have the object created and sized while it is being held down, then you need to change the 'scale the object' section to use 'Get$$anonymous$$ey' ins$$anonymous$$d of 'Get$$anonymous$$eyDown'. Get$$anonymous$$eyDown() returns true for only a single frame. Get$$anonymous$$ey() returns true all the time the key is held down.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with changing localScale of gameobject after instantiate 1 Answer

How do I scale an object or prefab to a size in Unity units? 1 Answer

Prefab instantiated wrong scale on mobile 0 Answers

Scale issue returns when object is instantiated 1 Answer

how do I scale my Instanciated GameObject to a parent gameObject that varies in scale? 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