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

Many CS problems :/

Discussion in 'Scripting' started by ThreeCents, Jul 26, 2018.

  1. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    I got 10 CS errors and two warnings that i can't resolve by myself : / ; (in C#)

    1- Assets/BallControl.cs(24,10): error CS1011: Empty character literal
    2- Assets/BallControl.cs(24,11): error CS1525: Unexpected symbol `GoBall'
    3- Assets/BallControl.cs(24,18): error CS1011: Empty character literal
    4- Assets/BallControl.cs(33,10): error CS1011: Empty character literal
    5- Assets/BallControl.cs(33,11): error CS1525: Unexpected symbol `GoBall'
    6- Assets/BallControl.cs(33,18): error CS1011: Empty character literal
    7- Assets/BallControl.cs(36,31): error CS1011: Empty character literal
    8- Assets/BallControl.cs(36,32): error CS1525: Unexpected symbol `Player'
    9- Assets/BallControl.cs(36,39): error CS1011: Empty character literal
    10- Assets/BallControl.cs(36,41): error CS1525: Unexpected symbol `)'
    (warning) 11- Assets/BallControl.cs(36,43): warning CS0642: Possible mistaken empty statement
    (warning) 12- Assets/BallControl.cs(13,6): warning CS0642: Possible mistaken empty statement

    I tried (and I am still trying) my best to find AND resolve theses errors. Any help is welcome ! Thanks !

    Here's my code ;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallControl : MonoBehaviour {
    6.  
    7.     private Rigidbody2D rb2d;
    8.  
    9.     void GoBall()
    10.     {
    11.         float rand = Random.Range(0, 2);
    12.         if (rand < 1);
    13.         {
    14.             rb2d.Addforce(new Vector2(20, -15));
    15.         }
    16.         else
    17.         {
    18.             rb2d.AddForce(new Vector2(-20, -15));
    19.         }
    20.     }
    21.  
    22.     void Start () {
    23.         rb2d = GetComponent<Rigidbody2D>();
    24.         Invoke(''GoBall'', 2);
    25.  
    26.     }
    27.     void ResetBall(){
    28.         rb2d.velocity = Vector2.zero;
    29.         transform.position = Vector.zero;
    30.     }
    31.     void RestartGame(){
    32.         ResetBall();
    33.         Invoke(''GoBall'', 1);
    34.     }
    35.     void OnCollisionEnter2D (Collision2D coll) {
    36.         if(coll.collider.CompareTag(''Player'')){
    37.             Vector2 vel;
    38.             vel.x = rb2d.velocity.x;
    39.             vel.y = (rb2d.velocity.y / 2) + (coll.collider.attachedRigidbody.velocity.y / 3);
    40.             rb2d.velocity = vel;
    41.         }
    42.     }
    43. }
    44.  
     
  2. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    It looks like in many lines you're using double apostrophes '' rather than an actual quotation symbol ". For example, this appears to be the case in line 24:

    Code (CSharp):
    1. Invoke(''GoBall'', 2);
    As a result, ''GoBall'' is being interpreted as (empty single character) (the name GoBall, which C# wasn't expecting) (another empty single character) rather than as the single string object "GoBall".

    Most of the errors seem to be of this same type, using doubled-up single apostrophes, which are used to define char's, not strings, rather than double quotation marks, which are used to define strings.
     
  3. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    I tried to replace the '' (double-up apostroph) with “” (quotation marks) but it didn't worked :/
     
  4. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    Do you mean you literally substituted "" wherever '' was?

    If so, that won't work, no. What you should do is use a single quotation mark " in each location where you were previously using doubled-up apostrophes:

    Code (CSharp):
    1. "GoBall" // this is a string
    2.  
    3. ''GoBall'' // this is an empty character, then an unrecognized name, then another empty character
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Will try to paste a working copy...

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallControl : MonoBehaviour {
    6.  
    7.     private Rigidbody2D rb2d;
    8.  
    9.     void GoBall()
    10.     {
    11.         float rand = Random.Range(0, 2);
    12.         if (rand < 1);
    13.         {
    14.             rb2d.Addforce(new Vector2(20, -15));
    15.         }
    16.         else
    17.         {
    18.             rb2d.AddForce(new Vector2(-20, -15));
    19.         }
    20.     }
    21.  
    22.     void Start () {
    23.         rb2d = GetComponent<Rigidbody2D>();
    24.         Invoke("GoBall", 2);
    25.     }
    26.  
    27.     void ResetBall(){
    28.         rb2d.velocity = Vector2.zero;
    29.         transform.position = Vector.zero;
    30.     }
    31.  
    32.     void RestartGame(){
    33.         ResetBall();
    34.         Invoke("GoBall", 1);
    35.     }
    36.  
    37.     void OnCollisionEnter2D (Collision2D coll) {
    38.         if(coll.collider.CompareTag("Player")){
    39.             Vector2 vel;
    40.             vel.x = rb2d.velocity.x;
    41.             vel.y = (rb2d.velocity.y / 2) + (coll.collider.attachedRigidbody.velocity.y / 3);
    42.             rb2d.velocity = vel;
    43.         }
    44.     }
    45. }
    Try copying & pasting that to see if it works. Report if any errors came from that.
     
    ThreeCents likes this.
  6. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    Ok thanks ! :)
     
  7. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    It worked but there is still 1 error and one warning :) But thanks anyway ! That really helped me :D The error is : Assets/BallControl.cs(16,8): error CS1525: Unexpected symbol `else' . And the warning is : Assets/BallControl.cs(13,9): warning CS0642: Possible mistaken empty statement.
     
  8. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    Thank you for your explanation :) !
     
  9. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Remove the ; in the following line (Line 12 in what methos gave you)
    Code (CSharp):
    1. if (rand < 1);
     
    ThreeCents likes this.
  10. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    It didn't worked, it just gave me more errors... Thanks anyway !
     
  11. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    That semicolon was very much misplaced. The other issues will be elsewhere.
    Please expand on 'more errors'.
     
    Last edited: Jul 27, 2018
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    What roykoma said was correct. Sometimes when you fix errors it will reveal more errors that were already there, but the compiler couldn't find before.
     
  13. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    Oooooh okay ! The errors were : Assets/BallControl.cs(14,18): error CS1061: Type `UnityEngine.Rigidbody2D' does not contain a definition for `Addforce' and no extension method `Addforce' of type `UnityEngine.Rigidbody2D' could be found. Are you missing an assembly reference? and : Assets/BallControl.cs(29,30): error CS0103: The name `Vector' does not exist in the current context.
     
  14. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    I just resolved the 2nd error (Assets/BallControl.cs(29,30): error CS0103: The name `Vector' does not exist in the current context.) by myself but I don't understand the other problem.
     
  15. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    TaleOf4Gamers likes this.
  16. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I highly recommend walking away from your Unity tutorials and taking up some general C# tutorials first before returning to Unity. The C# tutorials don't need to be Unity related.

    From a programming perspective, outside of the visual aspects of the editor, Unity is basically a complex set of C# API's you use to help create your game. You of course need to learn the language first before learning how to access the API's in that language.
     
    NeverConvex likes this.
  17. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    I also think that's a good suggestion. Most of the questions you're asking, NoSarcasmPlease, will be much easier for you to diagnose and understand if you pick up a bit of OO, strongly typed programming, which learning a fair bit of C# separately would necessarily involve.

    It is, of course, possible to learn to code while simultaneously figuring out the Unity API and UI, but I think it's probably harder. Unity has a lot more stuff going on that distracts from the core ideas behind coding in C#.
     
    Joe-Censored likes this.
  18. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    Yeah okay i'll do that ^^''
    Thanks you all for your replies !
     
    NeverConvex likes this.
  19. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    (It worked by the way thanks NeverConvex)
     
    NeverConvex likes this.