Here is the code that I have. I have this in a script attatched to an object in my scene.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using Unity.VisualScripting;
using UnityEngine;
public class AnimationHandler : MonoBehaviour
{
string thisLowPrioritySprite = "00";
string thisHighPrioritySprite = "00";
int Counter = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RealSpriteHandler.LowPrioritySprite = thisLowPrioritySprite;
RealSpriteHandler.HighPrioritySprite = thisHighPrioritySprite;
}
}
public class walkingAnimation
{
public class nextFrame {
Counter++;
}
}
The issue: Inside the WalkingAnimation class, I cannot get any variables to work. I have tried everything I can think of, from public to static to private variables, and Ive tried declaring them in literally every spot possible. Yet somehow, No matter what I do, no code within my WalkingAnimation class will ever recognize a variable.
I am assuming this is some quirk of custom classes that I don’t know about, so please enlighten me.
Custom non-Unity object classes need to be decorated with [System.Serializable]
for Unity to attempt to serialize them: Unity - Scripting API: Serializable
Look at the documentation I linked.
Hate to say it, the documentation has cleared nothing up. I’ve now tried serializing every different method, field, or variable, and I still cant get that class to reference any variables at all. I am completely lost.
You literally just need to put the attribute above your class:
[System.Serializable]
public class walkingAnimation
And then ensure the members in your class are serializable as normal. It’s spelled out plain as day in the documentation.
Of course you need to be serializing an instance of said class in your monobehaviour class as well.
Okay I realise now you probably don’t want to serialize walking animation, but have it interface with your AnimationHandler component.
A: It should probably inherit from MonoBehaviour itself. It will need to be in it’s own script file then.
B: You should have a serialized field for AnimationHandler
and reference said component via the inspector.
You can then access any public variables/methods in said component.
Spoiler alert - This does not fix the issue. I still cannot reference any variables. I already tried this bro. All I get for every variable is " Counter does not exist within the current context" even if i declare Counter on like 37 and try to reference it on line 38. No dice.
talking about serializing the fields
public class WalkingAnimation : MonoBehaviour
{
[SerializeField]
private AnimationHandler _handler; //reference via inspector
private void Update()
{
_handler.Counter++;
}
}
Which fields exactly?
In your example code you don’t actually use WalkingAnimation
anywhere. You have declared a class, though would still need an instance of it in some form. Said instance would also need a reference to AnimationHandler
in order to access any of it’s non-static members.
well I was getting there, but then I realized I cant get walkingAnimation to reference any variables. Theres no point in me implementing it if I cant get anything in it to come even remotely close to working.
But if I declare a variable within my class, I still cannot access it at all. I know that it should be seperate from the variables for the rest of the script. I cannot get it to work with any variables.
At this point your issues come down to not understanding how C# works.
I posted a simple script above showing how you reference and interact with another component. You need an instance of it, it needs to have a reference to the other object you care about, and then you can access it’s public, non-static members within valid contexts (such as a method).
Otherwise I can’t step you through the basics of C#. You ought to spend some time learning that.
I got all of that, and it does work. I am pretty new on C#, Ill admit. It just makes no sense to me how I can have a public class, and declare a variable, and then on the next line the variable seemingly doesn’t exist. Thank you for showing me how to reference the variables like that.
Because you were just writing invalid code. Counter
is a variable within another class. You can’t just use it without first having a reference to an object, and then accessing the member through said object, and doing so in a valid context such as a method.
I know I cant reference it from the other class. Even if I declare it in my own custom class the variable still will not be recognized. Thats what I do not understand.
just because you declare it with the same name doesnt mean its the same variable