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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] I have a problem with error CS0116

Discussion in 'Scripting' started by Kallael, Nov 22, 2016.

  1. Kallael

    Kallael

    Joined:
    Nov 21, 2016
    Posts:
    3
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class IA : MonoBehaviour {
    5.  
    6.     bool joueurVu = false;
    7.  
    8.     public GameObject joueur;
    9.  
    10.     void Start () {
    11.         joueur = gameObject.Find ("First Person Controller");
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.                 if (getDistance() < 5)
    17.         {
    18.                         joueurVu = true;
    19.                         print ("joueur vu !");
    20.         }
    21.     }
    22. }
    23.         return Vector3.Distance(joueur.Transform.position, Transform.position);
    24.     float getDistance(){
    25.     }
    26.    
    This is my code its says that at line 23 the error is and i dont know how to fix it
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You have a couple of close brace '}' to many.
     
  3. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    Well first of all your return statement has to be within the methods body

    and second your method has to be within a class


    Code (CSharp):
    1.      
    2. class IA : MonoBehaviour {
    3. //[...] Your other stuff
    4. float getDistance() {
    5.      return Vector3.Distance(joueur.Transform.position, Transform.position);
    6. }
    7.  
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Code (CSharp):
    1. return Vector3.Distance(joueur.Transform.position, Transform.position);
    2.     float getDistance(){
    3.     }
    4.  
    5. // should be
    6.  
    7.     float getDistance(){
    8.         return Vector3.Distance(joueur.Transform.position, Transform.position);
    9.  
    10.     }
     
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Don't you have an IDE that tells you where the problems are?

    Your getDistance() method doesn't return af float, and line 23 is outside of a function.
     
  6. Kallael

    Kallael

    Joined:
    Nov 21, 2016
    Posts:
    3

    So can you tell me what i have to write ? im starting in C# and unity 3d coding and i create this script for a pnj character who follow my hero when he gets close to him,
     
  7. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Check Timelog's last post.

    Also, check this link out.
     
  8. Kallael

    Kallael

    Joined:
    Nov 21, 2016
    Posts:
    3
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class IA : MonoBehaviour {
    5.  
    6.  
    7.     bool joueurVu = false;
    8.  
    9.     public GameObject joueur;
    10.  
    11.     void Start () {
    12.         joueur = gameObject.Find ("First Person Controller");
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.                 if (getDistance() < 5)
    18.         {
    19.                         joueurVu = true;
    20.                         print ("joueur vu !");
    21.         }
    22.     }
    23.     float getDistance(){
    24.         return Vector3.Distance(joueur.Transform.position, Transform.position);
    25.     }
    26. }
    this is the new code now i have 3 errors
    line 12 error cs0176
    The member 'UnityEngine.GameObject.Find (string)' is inaccessible with an instance reference; Qualify it with a type name (CS0176) (Assembly-CSharp)
    line 24 error cs1061
    'UnityEngine.GameObject' does not contain a definition for 'Transform' and no 'Transform' extension method accepting A first argument of type 'UnityEngine.GameObject' was found (a using directive or an assembly reference is missing?) (CS1061) (Assembly-CSharp)
    line 24 error cs0120
    An object reference is required for the property, method, or non-static field 'UnityEngine.Transform.position .get '(CS0120) (Assembly-CSharp)
     
  9. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Seems like you're starting with programming period. What I would do is follow some basic programming tutorials (check my sig) and learn those basics first. After that you should be able to figure these errors out yourself. Also next time please post the entire error and not just the code, it's not like we all know the entire error code list from the top of our heads ;)
     
  10. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    Write GameObject with a capital G in line 12 and transform with a lowercase t in line 24.

    And other than that I'd second the reply above to focus and working through tutorials
     
  11. heyiamcool

    heyiamcool

    Joined:
    Jan 14, 2021
    Posts:
    2
    I still cant fix it .I had over 141 complier errors and deleted my code only leaving a lowercase U not knowing what to do .I now have 2 complier errors one of them being cs0116 and the other
    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.CollectNamespace (UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser+Node parent) (at <2f1c602eae0d45c293fff3e3aef759fa>:0)
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.FindClassAndNamespace (System.String className, System.String source, System.Boolean acceptStruct) (at <2f1c602eae0d45c293fff3e3aef759fa>:0)
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.FindNamespace (System.String sourceCode, System.String className, System.Boolean acceptStruct, System.String[] defines) (at <2f1c602eae0d45c293fff3e3aef75
    9fa>:0)
    UnityEditor.Scripting.Compilers.CSharpLanguage.GetClassAndNamespace (System.String filePath, System.String definedSymbols, System.String& outClassName, System.String& outNamespace) (at <2f1c602eae0d45c293fff3e3aef759fa>:0)
    UnityEditor.Scripting.ScriptCompilers.GetClassAndNamespace (System.String file, System.String definedSymbols,
    please help
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    PLEASE don't reply to five-year-old threads. It's against forum rules. Start your own. It's free.

    When you post, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    Wolfenpixels101 likes this.