- Home /
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;
}
}
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.
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);
}
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.
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);
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);
}
}
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
Follow this Question
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