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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

hi, i have this problem and i am so rookie in c# sou can some one help me?

Discussion in 'Scripting' started by experttrolador, Feb 3, 2021.

?

how resolve? "error cs1514 and "error cs1513"

  1. ...

    1 vote(s)
    100.0%
  2. ...

    1 vote(s)
    100.0%
Multiple votes are allowed.
  1. experttrolador

    experttrolador

    Joined:
    Feb 3, 2021
    Posts:
    8
    1 using System.Collections;
    2 using System.Collections.Generic;
    3 using UnityEngine;
    4
    5 public class playerController : MonoBehaviour
    6 { public class speed float speed = 2f();
    7
    8 private class Rigidbody2D rb2d();
    9
    10 // Start is called before the first frame update
    11 void Start()
    12 {
    13 rb2d = GetComponent<Rigidbody2D>();
    14 }
    15
    16 // Update is called once per frame
    17 void Update()
    18 {
    19
    20 }
    21 void FixedUpdate()
    22 {
    23 float h = Input.GetAxis("Horizontal");
    24 rb2d.AddForce(Vector2.right * speed * h);
    25 }
    26 }
     
  2. caiovini980

    caiovini980

    Joined:
    Apr 6, 2020
    Posts:
    8
    Hey, experttrolador.
    I suppose that you're trying to make an GameObject move on 2D through it's Rigidbody2D, right?
    In that case I will make some changes on this code, it should look like this:

    Code (CSharp):
    1.  
    2.    using System.Collections;
    3.    using System.Collections.Generic;
    4.    using UnityEngine;
    5.  
    6.    public class playerController : MonoBehaviour
    7.    {
    8.     public float speed = 2.0f;
    9.  
    10.     private Rigidbody2D rb2d;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb2d = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.      // Update is called once per frame
    19.      void Update()
    20.      {
    21.  
    22.      }
    23.  
    24.     void FixedUpdate()
    25.     {
    26.         float h = Input.GetAxis("Horizontal");
    27.         rb2d.AddForce(Vector2.right * speed * h);
    28.     }
    29. }
    1 - On the top of the code, are the namespaces that your code would use to execute correctly, we can add more if we need.
    2 - Then comes the Class, in this case it's named playerController and it inherits from Monobehaviour class, what this means?
    Its means that now we can use some functions from the Monobehaviour class, just like that GetComponent<>() on the Start method!
    3 - After the Class declaration, are the variables. In your case you used the keyword 'class' to declarate them, but it's no need. We don't want to create another class right now, we want to declare our variables. So now, the variables are just {public/private} {the variable type} {how we will name it} and on the case of speed, it's {value}.

    The rest is just fine!
    I hope it solve your problem.
     
    DanielBrunt and experttrolador like this.
  3. experttrolador

    experttrolador

    Joined:
    Feb 3, 2021
    Posts:
    8
    thanks, caiovini980 that work
     
    caiovini980 likes this.