- Home /
Error CS0116 : A namespace can only contain types and namespace declarations. PLEASE HELP ME!
Here is my C# script that is attached to my coins. The error is on (4,6)
using UnityEngine;
using System.Collections;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Coin")
{
other.gameObject.SetActive = false;
}
}
Answer by taxvi · Dec 09, 2014 at 09:04 AM
you need to wrap a class around your OnTriggerEnter function like this:
using UnityEngine;
using System.Collections;
public class MyClassName : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Coin")
{
other.gameObject.SetActive = false;
}
}
}
Also SetActive is a function, not a property. Change the SetActive line for this:
other.gameObject.SetActive(false);
@taxvi Having the same issue but with another script. I tried the solution here and it didn't work.
Here is my script.
using UnityEngine; using System.Collections; using System.IO;
void Start (paraser) {
fileToParse = "$$anonymous$$acintosh HD/Users/chrisburke/Documents/Unity projects";
fileToParse = Path.Combine(fileToParse, "Walls for Unity FBX");
fileToParse = fileToParse + "." + txt;
FileInfo theSourceFile = null;
TextReader reader = null; // NOTE: TextReader, superclass of StreamReader and StringReader
// Read from plain text file if it exists
theSourceFile = new FileInfo (Path.Combine(Application.dataPath,fileToParse));
if ( theSourceFile != null && theSourceFile.Exists ){
reader = theSourceFile.OpenText(); // returns StreamReader
Debug.Log("Created Stream Reader for " + fileToParse + " (in Datapath)"
);
}
}
Your c# class is incorrect and you're missing the class definition, you have the using statements which is nice and all but it must be in a class and if it's going to be attached to gameobjects it needs to inherit/extend monobehaviour like this:
using UnityEngine;
using System.Collections;
using System.IO;
public class TheClassName : $$anonymous$$onoBehaviour
{
void Start(paraser)
{
fileToParse = "$$anonymous$$acintosh HD/Users/chrisburke/Documents/Unity projects";
fileToParse = Path.Combine(fileToParse, "Walls for Unity FBX");
fileToParse = fileToParse + "." + txt;
FileInfo theSourceFile = null;
TextReader reader = null; // NOTE: TextReader, superclass of StreamReader and StringReader
// Read from plain text file if it exists
theSourceFile = new FileInfo(Path.Combine(Application.dataPath, fileToParse));
if (theSourceFile != null && theSourceFile.Exists)
{
reader = theSourceFile.OpenText(); // returns StreamReader
Debug.Log("Created Stream Reader for " + fileToParse + " (in Datapath)"
);
}
}
}
If you intend on using Unity to call the Start method it will fail since you added a parameter to it. If that is not the case then you need to add what type the variable in the start parameters is, is it a string? should it even be there? etc.
It would be wise if you studied up on c# a bit as this issue is due to not understanding the syntax and expectations c# has.
Thanks i can see where i went wrong. Problem solved :)
Your answer
Follow this Question
Related Questions
Open door = load scene c# issues 4 Answers
Can't find source of trigger Null Reference Exception 1 Answer
Distribute terrain in zones 3 Answers
OnTriggerEnter2D/OnCollisionEnter2D - delay 1 Answer
Multiple Cars not working 1 Answer