[SOLVED] I have a problem with error CS0116

using UnityEngine;
using System.Collections;

public class IA : MonoBehaviour {

    bool joueurVu = false;

    public GameObject joueur;

    void Start () {
        joueur = gameObject.Find ("First Person Controller");
    }
   
    // Update is called once per frame
    void Update () {
                if (getDistance() < 5)
        {
                        joueurVu = true;
                        print ("joueur vu !");
        }
    }
}
        return Vector3.Distance(joueur.Transform.position, Transform.position);
    float getDistance(){
    }

This is my code its says that at line 23 the error is and i dont know how to fix it

You have a couple of close brace ‘}’ to many.

Well first of all your return statement has to be within the methods body

and second your method has to be within a class

class IA : MonoBehaviour {
//[...] Your other stuff
float getDistance() {
     return Vector3.Distance(joueur.Transform.position, Transform.position);
}
return Vector3.Distance(joueur.Transform.position, Transform.position);
    float getDistance(){
    }

// should be

    float getDistance(){
        return Vector3.Distance(joueur.Transform.position, Transform.position);

    }

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.

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,

Check Timelog’s last post.

Also, check this link out.

using UnityEngine;
using System.Collections;

public class IA : MonoBehaviour {


    bool joueurVu = false;

    public GameObject joueur;

    void Start () {
        joueur = gameObject.Find ("First Person Controller");
    }
   
    // Update is called once per frame
    void Update () {
                if (getDistance() < 5)
        {
                        joueurVu = true;
                        print ("joueur vu !");
        }
    }
    float getDistance(){
        return Vector3.Distance(joueur.Transform.position, Transform.position);
    }
}

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)

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 :wink:

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

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

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/assets-mouselook-cs-29-62-error-cs1003-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.

1 Like