Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot get my "Cube" to scale bigger while pressing a key.

Discussion in 'Scripting' started by Notarmaso, Sep 10, 2014.

  1. Notarmaso

    Notarmaso

    Joined:
    Oct 1, 2013
    Posts:
    13
    So this should we very simple. I am very new to Unity and I am trying to get my cube to move and also be able to change it scale when pressing my key P (Maybe also descale). (The movement is working).

    Here is my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeScript : MonoBehaviour {
    5.  
    6.     public float rotationSpeed;
    7.     private Vector3 direction;
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.                 float v = Input.GetAxis ("Vertical");
    16.                 float h = Input.GetAxis ("Horizontal");
    17.  
    18.                 direction = new Vector3 (h, v, 0);
    19.  
    20.                 //Input er det meste naar det er noget med styring at goere.
    21.                 if (Input.GetKey (KeyCode.LeftShift)) {
    22.  
    23.                         rotate ();
    24.                 }
    25.                 if (Input.GetKey (KeyCode.P)) {
    26.                     Bigger();
    27.          
    28.         movement (direction);
    29.         }
    30.  
    31.     void rotate(){
    32.                 transform.Rotate (direction, rotationSpeed * Time.deltaTime);
    33.  
    34.         }
    35.     void movement(Vector3 movementDirection){
    36.         transform.position += movementDirection * Time.deltaTime;
    37.         }
    38.         void Bigger(){
    39.             transform.localScale += new Vector3(0.5f,0,0);
    40.             }
    41.  
    42.      
    43.     }
    I am also getting an error telling me "Keyword `void' cannot be used in this context" and an Unexpected Symbol Error.

    -Thanks!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    Missing one: }
     
  3. Notarmaso

    Notarmaso

    Joined:
    Oct 1, 2013
    Posts:
    13
    Can you tell me where? Because if I add one in the end it give me a 3rd error, giving me a parsing error.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    I think the error message is giving you a hint there already : )
     
    Chambers likes this.
  5. Chambers

    Chambers

    Joined:
    Apr 4, 2011
    Posts:
    70
    Line 27 is where you want an extra curly brace if you didn't find this already, mgear was right though learning to use the errors can really save time. This is a really good example as to why following the following convention is helpful.
    Code (CSharp):
    1. void DoSomething()
    2. {
    3.     Debug.Log("Curly braces on new lines make debugging fun.");
    4. }
    All personal preference of course but I personally find that the best way to keep track of curly braces, especially as things get more complex.
     
    Notarmaso likes this.
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    A few more suggestions.

    Indentation matters. I'm guessing your original code was indented but copy-pasting into the forum screwed it all up. I personally have a hard time reading code that isn't indented properly.

    MonoDevelop and VisualStudio have ways to auto format code. In MonoDevelop I have the tab key set to auto format code. If my indentation suddenly changes, usually it's because I missed a brace or semicolon somewhere.

    When I type any kind of matching brace, I have the habit of typing the open and end brace at the same time. I then fill in the middle with code. Typing the end brace after the open brace is just automatic for me now so I rarely end up missing braces.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I'll pre-empitvely answer your next question. It's probably going to be "Why is it getting so big so fast?" That's because it's adding 0.5 to x -every frame-. If you want it to only happen on the first frame when the key is pressed, then use Input.GetKeyDown.

    If you do want it to continuously grow while the button is held, it's good practice to multiply it by Time.deltaTime:
    Code (csharp):
    1. transform.localScale+=new Vector3(0.5f * Time.deltaTime,0,0); //will grow by 0.5 units per second
    Anytime you're doing something continuously over every frame, and it's additive (as this is), you want to multiply in Time.deltaTime. That value will be bigger on longer frames and smaller on shorter ones, so no matter what the framerate is, a box growing by (0.5f * Time.deltaTime) will always grow by 0.5 units per frame. This gives your users a consistent and predictable experience.
     
    Notarmaso and flaminghairball like this.
  8. Notarmaso

    Notarmaso

    Joined:
    Oct 1, 2013
    Posts:
    13