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

Why Doesnt It Work??

Discussion in 'Editor & General Support' started by Witherslayer223, Mar 12, 2021.

  1. Witherslayer223

    Witherslayer223

    Joined:
    Feb 20, 2021
    Posts:
    6
    Im a hobbyist, and am trying to create a unity game in free, and I dont know why this code is not working..

    Any Help? I Got Error CS1061, it says "transform does not have a definition for find"
    Here is the code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using CodeMonkey.Utils;

    public class ToMenuButton : MonoBehaviour
    {
    private void Awake()
    {
    {
    transform.find("Tomenubtn").getcomponent<Button_UI>().ClickFunc = () => {
    Debug.Log("ClickedToMenu");
    Loader.Load(Loader.Scene.Menu);
    };
    }
    }

    As you can see im using code monkey utils, and my menu button is named "Tomenubtn"

    Please Help!
     
  2. midsummer

    midsummer

    Joined:
    Jan 12, 2017
    Posts:
    33
    C# is case-sensitive. That means that transform is not the same thing as Transform. Same goes for the names of the functions: getcomponent won't work, it's GetComponent. Also try Find with an uppercase F.

    Follow the suggestions your code editor gives you as you type (I assume you're using Visual Studio). If you can't see transform.find in the list of suggestions, it's usually because it doesn't exist.

    I'd advise checking out some very basic C# tutorials before jumping into making a game using Code Monkey's utilities (although I'm not familiar with the tutorials Code Monkey offers involving those utilities). Learning the very basics of the language properly before attempting more complicated stuff will save you from a lot of frustration (I know from experience).


    Ps. Use the "insert code" button when pasting code to the forums. Makes it much easier for others to read. This topic would perhaps also be better suited for the Scripting forum, as it's not really an editor issue as such.
     
  3. Witherslayer223

    Witherslayer223

    Joined:
    Feb 20, 2021
    Posts:
    6
    When i do this awnser, it says it "expected" a "}"
    i see three "{" and see three "}" so i am not sure what is wrong again
    Also here is insert code for you to read it better

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CodeMonkey.Utils;
    5.  
    6. public class ToMenuButton : MonoBehaviour
    7. {
    8.     private void Awake()
    9.     {
    10.         {
    11.             transform.find("Tomenubtn").getcomponent<Button_UI>().ClickFunc = () => {
    12.             Debug.Log("ClickedToMenu");
    13.             Loader.Load(Loader.Scene.Menu);
    14.         };
    15.     }
    16. }
     
  4. midsummer

    midsummer

    Joined:
    Jan 12, 2017
    Posts:
    33
    You miscounted your curly brackets. One of them is also in the wrong place – the { above the line beginning with "transform" should not be there. The capitalization errors I mentioned in the previous post are also still in the code.

    Things like "ClickFunc = () =>" (which is known as a lambda expression) can be very difficult for beginners to understand, so I'd recommend starting from closer to square one before trying to figure this out.

    Perhaps with this:
    https://unity3d.com/learning-c-sharp-in-unity-for-beginners
     
    CrandellWS likes this.
  5. Witherslayer223

    Witherslayer223

    Joined:
    Feb 20, 2021
    Posts:
    6
    i fixed the transform, that was an early copy+paste, i got this from code monkeys tutorial on scene loader, also my "loader" is called mainmenucontrol , i fixed this and now i have "mainmenucontrol.scene does not have a definition for menu" "menu" is my menu scene. also i will read the script tutorial.

    Updated Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CodeMonkey.Utils;
    5.  
    6. public class ToMenuButton : MonoBehaviour
    7. {
    8.     private void Awake() {
    9.             transform.Find("Tomenubtn").GetComponent<Button_UI>().ClickFunc = () => {
    10.             Debug.Log("ClickedToMenu");
    11.             MainMenuControl.Load(MainMenuControl.Scene.Menu);
    12.         };
    13.     }
    14. }
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Let's all try our level best to rise above the "I fixed one character and now there's a new error! OMG!!!!" loop.

    I'm gonna give you a little secret here: YOU can actually read these errors and see almost precisely to the character where the problem is, and possibly even google for more results.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
    CrandellWS likes this.
  7. Witherslayer223

    Witherslayer223

    Joined:
    Feb 20, 2021
    Posts:
    6

    Unity 3d wont work, this is a 2d game.

    Thanks for the forums though.

    Edit: Im So Dumb, sometimes i saw errors, and then moved on to another tutorial. i didnt even know that the numbers in the first characters were pinpointing the where the error was, i didnt know what it was, but thank you
     
    Last edited: Mar 12, 2021
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Awesome, glad you're back on track... and let me congratulate and encourage you to keep trying lots and lots of tutorials. It does get easier, really it does, but you will also find lots and lots of errors along the way, so don't get discouraged. YOU CAN FIX THEM! We're all counting on you WitherSlayer! :)
     
    CrandellWS likes this.
  9. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    177