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. Dismiss Notice

Error in C# script for setting up a trigger to prompt an image to show

Discussion in '2D' started by kl852, May 4, 2021.

  1. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Hi all,

    I have been struggling for four days with writing a line of code for a 2D mini game.

    My idea is:
    1. Setting up a cube
    2. When the Player step on the cube, it will prompt another Game object to show
    3. the game object itself can be shut down by space bar.

    I have edited the code for the cube for dozens of times but still seeing errors, I am so frustrated...
    Would anyone explain why CS0246 and the error "OnGUIDepth changed: was 0 is 1. Event type was 0" occured? Thanks <3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameObjShow : MonoBehaviour
    6. {
    7.     public gameObject customImage;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         customImage.SetActive(false);
    13.     }
    14.     void OnTriggerEnter2D(Collider2D other)
    15.     {
    16.         if (other.gameObject.tag == "Player");
    17.         {
    18.             customImage.SetActive(true);
    19.             Debug.Log("On trigger enter 2D");
    20.         }
    21.     }
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.      
    26.     }
    27. }
    28.  
    Do I need to define gameObject in the script? I thought it was in the "dictionary" of C# and not need to be defined...

    Thank you so much for your help!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    kl852 and MelvMay like this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Here's the explanation found via Google search (first result): https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0246

    The first thing listed is:
    Did you misspell the name of the type or namespace? Without the correct name, the compiler cannot find the definition for the type or namespace. This often occurs because the casing used in the name of the type is not correct. For example, Dataset ds; generates CS0246 because the s in Dataset must be capitalized.
     
    kl852 likes this.
  4. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Thanks! I have replaced "gameObject" with "GameObject".
    There's a new error message "Assets\Scripts\GameObjShow.cs(16,19): error CS1061: 'Collider2D' does not contain a definition for 'GameObject' and no accessible extension method 'GameObject' accepting a first argument of type 'Collider2D' could be found (are you missing a using directive or an assembly reference?)"

    :(
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Ugh, deleted my post. Meant to correct it. Damn my admin access.

    So you should learn how to use the docs TBH. Take a look at the "Collider2D" API. No "GameObject" but there's a "gameObject" property. Properties start in lowercase. C# is case-sensitive.

    BTW: Note that this isn't a 2D related issue, it's just scripting stuff best posted here.
     
    kl852 likes this.
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
  7. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Thanks for your help.
    I was using gameObject but there was another error.

    Assets\Scripts\GameObjShow.cs(7,12): error CS0246: The type or namespace name 'gameObject' could not be found (are you missing a using directive or an assembly reference?)

    TBH I have been looking at the docs for days but there are too much information that I don't know which one suits my case since I have zero knowledge in C#. The last couple of weeks I could write scripts for character walking and detecting colliders, changing scene and animation, produced art work for the game as well, sadly I am completely new to C#, hope you will understand the challenge I face...
     
  8. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Thanks!
    Class is more suitable since there's a field for drag and drop the appropriate object, however, whether I use Class and Object, there are still errors...
    Appreciate if you can point out what is the error in the script, thank you so much!
     
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    check this one, it should help:


    let me know how it goes?
     
    kl852 likes this.
  10. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Thanks for sharing the link!
    Most grateful if you can give some hints which part of the script or game play is relevant to variables and functions- so I can understand from the correct point of view, thanks.
     
  11. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    this is one (public) variable, right now its missing proper type:
    1. public gameObject customImage;
     
    kl852 likes this.
  12. kl852

    kl852

    Joined:
    Oct 12, 2020
    Posts:
    10
    Thanks! I think the issue has been addressed by changing the two game objects into Class and object!!
    upload_2021-5-4_11-30-19.png

    Tested it and the script manage to prompt the object to show :)

    Regards,
    Kelly
     
    mgear likes this.