Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Davoid ZalaX · Dec 09, 2014 at 08:45 AM · c#errortriggerpickupcoin

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3

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;
          }
     }
 }
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andres-Fernandez · Dec 09, 2014 at 09:07 AM 3
Share

Also SetActive is a function, not a property. Change the SetActive line for this:

 other.gameObject.SetActive(false);
avatar image oakwoodengineering · Nov 15, 2016 at 08:10 PM 0
Share

@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)"
     );
 }

}

avatar image Landern oakwoodengineering · Nov 15, 2016 at 08:14 PM 0
Share

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.

avatar image oakwoodengineering Landern · Nov 17, 2016 at 12:26 PM 0
Share

Thanks i can see where i went wrong. Problem solved :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges