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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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:
    33,663
    Ryiah likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    18,907
    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. }