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

Sitting?

Discussion in 'Getting Started' started by KingLlama, May 10, 2022.

  1. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    198
    I'm a total newb when it comes to scripting and have found this code for setting up a animation to play when the player presses a button. I set up the trigger animation but now i'm receiving this error.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.      public Transform animationPos;
    6.      public Animator anim;
    7.      public Transform player;
    8.      private void OnTriggerStay(Collider other)
    9.      {
    10.          if (Input.GetMouseButtonDown(0))
    11.          {
    12.              player.position = animationPos.position;
    13.              player.rotation = animationPos.rotation;
    14.              anim.SetTrigger("isSitting");
    15.          }
    16.      }
    17.  
    Assets\Sitting.cs(5,23): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\Sitting.cs(6,22): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\Sitting.cs(7,23): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\Sitting.cs(9,19): error CS0116: A namespace cannot directly contain members such as fields or methods
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I believe you forgot the class definition. You might want to compare to a new script, right click in the right pane in Project view and choose Create/C# Script to create an empty script that has the proper class definition.
     
    KingLlama likes this.
  3. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    198
    So, I did what you said and added the correct class but now i'm getting this. Maybe this is too advanced for me.

    Assets\Sitting.cs(6,41): error CS1514: { expected
    Assets\Sitting.cs(6,41): error CS1513: } expected

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.FindClassAndNamespace (System.String className, System.String source, System.Boolean acceptStruct) (at <d7545a46516941d4b2f2dec578cd41ee>:0)
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.FindNamespace (System.String sourceCode, System.String className, System.Boolean acceptStruct, System.String[] defines) (at <d7545a46516941d4b2f2dec578cd41ee>:0)
    Rethrow as IllegalNamespaceParsing: Searching for classname: 'Sitting' caused error in CSharpNameParser
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.FindNamespace (System.String sourceCode, System.String className, System.Boolean acceptStruct, System.String[] defines) (at <d7545a46516941d4b2f2dec578cd41ee>:0)
    UnityEditor.Scripting.ScriptCompilation.CSharpNamespaceParser.GetClassAndNamespace (System.String sourceCode, System.String className, System.String& outClassName, System.String& outNamespace, System.String[] defines) (at <d7545a46516941d4b2f2dec578cd41ee>:0)
    UnityEditor.Scripting.Compilers.CSharpLanguage.GetClassAndNamespace (System.String filePath, System.String definedSymbols, System.String& outClassName, System.String& outNamespace) (at <d7545a46516941d4b2f2dec578cd41ee>:0)
    UnityEditor.Scripting.ScriptCompilers.GetClassAndNamespace (System.String file, System.String definedSymbols, System.String& outClassName, System.String& outNamespace) (at <d7545a46516941d4b2f2dec578cd41ee>:0)
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    It would difficult to tell without seeing your code. Did you start with Create/C# Script? Get an empty script working first. Make sure your class name is the same as the filename. Start with the basics, then add your code and build on success!
     
  5. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    198
    Here is the code, The big error has disappeared after finding a space in the public class.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.     public class Sitting: MonoBehaviour
    7.      public Transform animationPos;
    8.      public Animator anim;
    9.      public Transform player;
    10.      public void OnTriggerStay(Collider other)
    11.     {
    12.          if (Input.GetMouseButtonDown(0))
    13.          {
    14.              player.position = animationPos.position;
    15.              player.rotation = animationPos.rotation;
    16.              anim.SetTrigger("isSitting");
    17.          }
    18.      }
    19.  
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    You did not start with a right click of your mouse and choosing Create/C# Script, you are still missing brackets.
     
  7. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    198
    I did, but i'm lost on where the brackets go because, on the new script it shows

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class test : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.        
    17.     }
    18. }
    19.  
    So, I fixed it I think, I put it the bracket after the public class.
     
    JeffDUnity3D likes this.
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I meant actually to just continue with the code exactly above, then add your code a bit at a time. Get things working first and build on success rather than chasing bugs.
     
    KingLlama likes this.