Search Unity

there's a problem with my code

Discussion in 'Scripting' started by matt123abc, Jun 9, 2022.

  1. matt123abc

    matt123abc

    Joined:
    Jun 9, 2022
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerControls : MonoBehaviour

    [Header("Game Controller Object for controlling the game")]
    public GameController gameController;
    [Header("Default Velocity")]
    public float velocity = 1;

    private Rigidbody2D rb;

    private float objectHeight;
    {

    void Start()
    {

    gameController = GetComponent<GameController>();

    Time.timeScale = 1;
    rb = GetComponent<Rigidbody2D>();

    objectHeight = transform.GetComponent<SpriteRenderer>().bounds.size.y / 2;
    }


    void Update()
    {

    if (Input.GetMouseButtonDown(0))
    {

    rb.velocity = Vector2.up * velocity;
    }
    }
    }
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Nice that you have got a problem.

    Try to describe your problem, so we can help you?
    Use Code Tags so we can read your code right?

    I Already see your typo, would love to answer after you update your Post ;)
     
    Ryiah likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Ryiah likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,157
    The curly brace needs to come directly after the class definition above where you defined the variables. You currently have it after the variable definitions.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControls : MonoBehaviour
    6. {
    7.     [Header("Game Controller Object for controlling the game")]
    8.     public GameController gameController;
    9.     [Header("Default Velocity")]
    10.     public float velocity = 1;
    11.  
    12.     private Rigidbody2D rb;
    13.  
    14.     private float objectHeight;
    15.  
    16.     void Start()
    17.     {
    18.    
    19.         gameController = GetComponent<GameController>();
    20.  
    21.         Time.timeScale = 1;
    22.         rb = GetComponent<Rigidbody2D>();
    23.  
    24.         objectHeight = transform.GetComponent<SpriteRenderer>().bounds.size.y / 2;
    25.     }
    26.  
    27.  
    28.     void Update()
    29.     {
    30.    
    31.         if (Input.GetMouseButtonDown(0))
    32.         {
    33.        
    34.             rb.velocity = Vector2.up * velocity;
    35.         }
    36.     }
    37. }