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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour
{
public int taskCount;
public float pointCount;
public Task task;
void Start()
{
task = new Task(5f);
}
public void UpdatePoints(float pointdiff)
{
pointCount += pointdiff;
}
}
[System.Serializable]
public class Task
{
float points;
public Task(float ipoints)
{
this.points = ipoints;
UpdatePoints(ipoints);
}
}
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:
public class Task {
private float points;
public Task(float points) {
this.points = points;
}
public float Points => points;
}
public class Manager : MonoBehaviour {
public Task task;
public float pointCount;
void Start() {
task = new Task(5f);
pointCount += task.Points;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public delegate void UpdatePointsEventHandler(float points);
public class Manager : MonoBehaviour
{
public int taskCount;
public float pointCount;
public Task task;
void Start()
{
task = new Task(5f, UpdatePoints);
}
public void UpdatePoints(float pointdiff)
{
pointCount += pointdiff;
}
}
[System.Serializable]
public class Task
{
float points;
public event UpdatePointsEventHandler onUpdatePoints;
public Task(float ipoints, UpdatePointsEventHandler onUpdatePoints)
{
this.points = ipoints;
this.onUpdatePoints += onUpdatePoints;
if(onUpdatePoints != null)
onUpdatePoints(ipoints);
}
}
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);
}