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

[Solved] How to set up a Class Instance

Discussion in 'Scripting' started by CobaltTheFox, Jul 16, 2021.

  1. CobaltTheFox

    CobaltTheFox

    Joined:
    May 29, 2015
    Posts:
    27
    Hello,

    I have set up a simple class with a name variable and a method to update that variable.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player
    6. {
    7.     public string name;
    8.  
    9.     public Player()
    10.     {
    11.         this.name = "";
    12.     }
    13.  
    14.     public void UpdateName(string val)
    15.     {
    16.         this.name = val;
    17.     }
    18. }
    19.  
    And I am working on creating a UI that can edit an instance of this class, and I have it set up this way.
    upload_2021-7-15_23-55-19.png
    So if I make an instance of this class in a script attached to Canvas, how would I call that instance's UpdateName method on the InputField's OnEndEdit() event? I appreciate any help, sorry if I'm not explaining what I want well, I'm experimenting with classes for the first time.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    This is actually a thing you will NEVER do in Unity. The reasons are:

    a) nothing is "attached" to Canvases

    b) Components are added to GameObjects using AddComponent<T>() method (Canvases are incidentally a type of Component)

    c) classis can be MonoBehaviours (which are a type of Component) and need to be derived from that base in order to be used by AddComponent<T>()

    d) You will NEVER call new on a MonoBehaviour, only AddComponent<T>();, where Unity will make it for you

    You probably want to go check out some basic scripting tutorials before you waste a lot of time trying to write code as if Unity doesn't exist and go straight C#. All the C# stuff exists, but anything that interoperates with Unity, which lives completely outside of C#, needs to be done properly using the Unity API, or it will be dead on arrival.

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    Imphenzia: How Did I Learn To Make Games:

     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Either Kurt misread, or I am, but you'll need to have an intermediary method that handles the event and manipulates your instance. Create a NamePageWindow MonoBehaviour that's attached to your NamePage object with an OnNameEntered method that your InputField invokes for your onEndEdit. During that method, find the object (either linked in the inspector or a static/singleton, whatever) with the Player instance and call UpdateName().
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
  5. CobaltTheFox

    CobaltTheFox

    Joined:
    May 29, 2015
    Posts:
    27
    Thank you all for your help. I tried GroZZleR's method and it worked well. I'm going to try Hikiko66's suggestion also. Thank you all for the help.