Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Get Reference in Type Class?

Discussion in 'Scripting' started by fel3web, Aug 22, 2020.

  1. fel3web

    fel3web

    Joined:
    Mar 16, 2020
    Posts:
    19
    I have a type "Task" and from there i need to be able to edit a variable "pointCount" in my gamemanager.
    If you want to see my attempt at making this work here it is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Manager : MonoBehaviour
    6. {
    7.     public int taskCount;
    8.     public float pointCount;
    9.     public Task task;
    10.     void Start()
    11.     {
    12.         task = new Task(5f);
    13.     }
    14.     public void UpdatePoints(float pointdiff)
    15.     {
    16.         pointCount += pointdiff;
    17.     }
    18. }
    19. [System.Serializable]
    20. public class Task
    21. {
    22.     float points;
    23.     public Task(float ipoints)
    24.     {
    25.         this.points = ipoints;
    26.         UpdatePoints(ipoints);
    27.     }
    28. }
     
    Last edited: Aug 22, 2020
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm assuming it's the "points" variable you want to edit outside of the Task object?

    Either make it a public field...
    public float points;

    ...Or a public auto-property:
    public float Points { get; set; }


    Edit, OP was updated with more info:

    If "points" is a constant value, you could give it a get-only public property and just update the value of "pointCount" in your Manager script when you instantiate a new Task:
    Code (CSharp):
    1. public class Task {
    2.    private float points;
    3.  
    4.    public Task(float points) {
    5.       this.points = points;
    6.    }
    7.  
    8.    public float Points => points;
    9. }
    Code (CSharp):
    1. public class Manager : MonoBehaviour {
    2.    public Task task;
    3.    public float pointCount;
    4.  
    5.    void Start() {
    6.       task = new Task(5f);
    7.       pointCount += task.Points;
    8.    }
    9. }
     
    Last edited: Aug 22, 2020
  3. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You could use a custom eventhandler to do this.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public delegate void UpdatePointsEventHandler(float points);
    6.  
    7. public class Manager : MonoBehaviour
    8. {
    9.     public int taskCount;
    10.     public float pointCount;
    11.     public Task task;
    12.     void Start()
    13.     {
    14.         task = new Task(5f, UpdatePoints);
    15.     }
    16.     public void UpdatePoints(float pointdiff)
    17.     {
    18.         pointCount += pointdiff;
    19.     }
    20. }
    21.  
    22. [System.Serializable]
    23. public class Task
    24. {
    25.     float points;
    26.     public event UpdatePointsEventHandler onUpdatePoints;
    27.  
    28.     public Task(float ipoints, UpdatePointsEventHandler onUpdatePoints)
    29.     {
    30.         this.points = ipoints;
    31.  
    32.         this.onUpdatePoints += onUpdatePoints;
    33.  
    34.         if(onUpdatePoints != null)
    35.             onUpdatePoints(ipoints);
    36.     }
    37. }
     
    Last edited: Aug 22, 2020
  4. fel3web

    fel3web

    Joined:
    Mar 16, 2020
    Posts:
    19
    Thanks for this! i interpreted a tiny snippet from your code to suit my needs more easily by passing the manager reference through the constructor. GENIUS!
    public Task(float ipoints, Manager imanager)
    {
    this.points = ipoints;
    this.manager = imanager;
    manager.UpdatePoints(ipoints);
    }