#Monodeath's Code Error

1 messages · Page 1 of 1 (latest)

topaz kraken
#

here you go!

#

@sharp sun

sharp sun
#

Thanks!

#

So to upload my code do I just like

#

put it on a google doc?

topaz kraken
#

```csharp

sharp sun
#

and share?

topaz kraken
#

nope

sharp sun
#

how do I share the code with you all lol

topaz kraken
#

```csharp
your code
```

sharp sun
#

oh ok

topaz kraken
#
//mycode
#

yup discord is great for code

sharp sun
#

```csharp
Test
```

topaz kraken
#

nope

#

without the back slashes

sharp sun
#

alr im sorry

#

ohhh

#

ok

topaz kraken
#

yeah, backslash is only to help people see how to do code commenting

sharp sun
#
Test
#

Perfect

#

Alr gimme one sec

topaz kraken
#

yeah you got it

#

side note, asciidoc instead of csharp is a great way to take notes in discord

sharp sun
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private float horizontal;
    [field: SerializeField]
    private PlayerStatsScriptableObject speed;
    private float jumpingPower = 16f;
    private bool isFacingRight = true;

    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
        }

        if(Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }

        Flip();
    }

    //speed = (PlayerStatsScriptableObject)ScriptableObject.CreateInstance(typeof(PlayerStatsScriptableObject));

    private void FixedUpdate()
    {
        speed = (PlayerStatsScriptableObject)ScriptableObject.CreateInstance(typeof(PlayerStatsScriptableObject));
        rb.velocity = new Vector2(horizontal * PlayerStatsScriptableObject.speed, rb.velocity.y);
    }

    private bool IsGrounded()
    {
        return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    }

    private void Flip()
    {
        if(isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
        {
            isFacingRight = !isFacingRight;
            Vector3 localScale = transform.localScale;
            localScale.x *= -1f;
            transform.localScale = localScale;
        }
    }
}

topaz kraken
#
= header =
* list item 1
* list item 2
thingy :: thingy
thingy :: thingy
= another header = 
[important]
blah blah blah
#

so, whats the issue?

sharp sun
#

so I’m trying to use a ScriptableObject to make changeable stats

topaz kraken
#

oh i figured it out already

sharp sun
#

Yep

#

jesus you’re smart 😅

topaz kraken
#

wait, no thats discord confusing me with indentation, hold on

#

i thought you were declaring private methods inside of update then lmao

#

had to open the thread fullscreen instead of split screen 😂

sharp sun
#

Ok so I’m trying to make a stat system with Scriptable Objects, but the console keeps saying “an object reference is required for the non-static field, method, or property ‘PlayerStatsScriptableObject.speed’

#

and it won’t actually change the speed of the player when I edit the scriptable object’s speed

topaz kraken
#

an object reference is required, that means the thing doesnt actually exist yet in your code

#

youve declared that an object CAN exist, but it hasnt been hooked up to the script yet

sharp sun
#

ohh

#

that makes a lot of sense

topaz kraken
#

its non static, thats because you can make multiple of them

#

with their own settings

sharp sun
#

so how do I fix it?

topaz kraken
#

so you need to either, on awake, or on start, actually initialize that scriptableObject

#

so that the code doesnt try to read an empty reference

sharp sun
#

sorry if I’m wrong on any part

i would put above the void Update function

onAwake()
CreateInstance.PlayerStatsScriptableObject
topaz kraken
#

oh, theres a better way

#

make it public, not private

sharp sun
#

which var

topaz kraken
#

then just drag and drop the object into the inspector reference

#

the scriptableObject

#

then in the inspector area, it will appear, you can drop the scriptableObject from your assets straight onto it

sharp sun
#

ok I made it public

#

where would it appear under?

topaz kraken
#

the script itself, the one youve edited

sharp sun
#

Sorry if I’m bad at this, I started Unity roughly 2 hours ago 😅

topaz kraken
#

thats alright, you done c# before?

sharp sun
#

nope

#

my first time

topaz kraken
#

oh this is gonna be a learning curve

sharp sun
#

yeah haha

topaz kraken
#

any other programming languages?

sharp sun
#

not really

#

I know for a fact my old scratch games do not count

#

😂

topaz kraken
#

lemme give a short lesson to explain in OOP terms

#
public int MyInteger;
private int MyPrivateInteger;
#

the difference here is that the private one can only have its value changed by code inside the same file its in

sharp sun
#

Yeah I know how Private and Public data works

#

as well as Integers/Variables in general

#

sorry, I probably should’ve mentioned that

topaz kraken
#

okay, in unity though, when something is public, the actual inspector area creates a field you can edit for it

#

a great example here

sharp sun
#

alr so

#

hold om

#

hold on

#

i have the inspector opened

#

and the script selected

topaz kraken
#
[Range(0, 1)] public float myRange;```
sharp sun
#

and I don’t see the ScriptableObj

#

wait

#

i think I do?

topaz kraken
#

screenshot?

sharp sun
#

uhh

topaz kraken
#

win+shift+s if you dont know the fast screencap trick

sharp sun
#

is a phone picture ok? 😅

#

oh alr

topaz kraken
#

then just paste, no need to save it

sharp sun
topaz kraken
#

no no

#

i mean yeah, but not quite

sharp sun
#

oh did you want

#

the whole screen

topaz kraken
#

click on the object in your sceneHierarchy

sharp sun
#

oh the Player?

topaz kraken
#

(i just wanted to see if you were looking in the right place)

#

yup

#

then you will see the player script has a new box on it

#

you can drop the scriptableObject into that box

sharp sun
#

uhhh

#

im a little lost

#

sorry

#

So I’m on the player

#

But I don’t see any new boxes

topaz kraken
#

yeah, has the player got a script?

sharp sun
#

Yes

topaz kraken
#

can i see the inspector for PlayerMovement

sharp sun
#

PlayerMovement.cs

#

ofc

topaz kraken
#

well, the inspector for the player, where the PlayerMovement component is attached

sharp sun
#

wait

#

nevermind

#

im dense

#

i just got what you said hahah

#

Alr I see it

topaz kraken
#

oh?

#

making sense yeah?

sharp sun
#

This is what you meant, right?

#

that box on the top?

topaz kraken
#

nope, thats the script itself

#

i mean the player

#

but its attached PlayerMovement component

sharp sun
#

damnit thats what i was looking at originally XD

#

alr

#

this one?

topaz kraken
#

yup

sharp sun
#

also thank you for being so patient with me, I must be infuriating to deal with

#

Oh that

#

yes

topaz kraken
#

this is where you choose which ScriptableObject you want to attach

#

the players own ( movement scripts own ( speed ScriptableObject ) )

#

does that make sense?

sharp sun
#

yes

#

Idk if I should mention this, but I made the scriptable object in a different script

#

not in the PlayerMovement

topaz kraken
#

thats exactly its purpose

sharp sun
#

Yeah I thought so, but I wanted to be sure you knew everything

#

just in case

topaz kraken
#

think of a scriptable object as a set of alternate settings for things

sharp sun
#

Ok

topaz kraken
#

like if you make a generic playercontroller that depends on a bunch of settings, and are building a clone of overwatch, for example, each character can have its own ScriptableObject to make them unique

#

its not the way id go about it in production code, but i think the concept makes sense

sharp sun
#

ok so

#

I want each character to run off of one scriptableobject

#

Let me explain

topaz kraken
#

so they all share move speeds and such, rather than manually setting them all?

#

NPCs and playerCharacter, i assume

sharp sun
#

They share the same data types, I was using the scriptable object to change them between classes

#

So for example

#

One class is Leaper

#

They have a normal speed

#

but a high jump height

#

and another one

#

is Speedster

#

they have a normal Jump Height

#

but high speed

#

i was trying to use the ScriptableObject to change the values with each character

#

if that makes sense

topaz kraken
#

if its just values you are going for, you could probably have gotten away with a static struct

sharp sun
#

I’m also trying to change things like the art that appears in the GUI

#

but in general

topaz kraken
#

but, yeah

#

i was going to expand in that direction

sharp sun
#

oh, sorry

topaz kraken
#

no problem you just beat me to the same point lol

#

but yeah, for values its minor overkill, but its never a bad idea

#

you can always expand the functionality without needing to overhaul any designs

#

excellent for 2 hours of work tbh

#

i know paid people who dont do this

sharp sun
#

So if I was just trying to make each class have a unique set of variables, would I use a ScriptableObject or no?

sharp sun
topaz kraken
#

np!

#

scriptableObject is ok, but its mostly for actual functionality

#

for instance, we could just use a struct to handle the data here perfectly fine

#

in overwatch, each characterClass has its own special ability right

sharp sun
#

yeah

#

ohh

#

i get it

#

for actual functions and unique things that aren’t shared between players

topaz kraken
#

in this case, yeah, we can ask a ScriptableObject to do its "special"

sharp sun
#

you want a ScriptableObject

topaz kraken
#

each version of that ScriptableObject can have its own functionality for its own special, its a neat thing

sharp sun
#

That makes so much more sense

#

uh, would you mind me asking how to make a struct?

#

if you aren’t busy ofc

topaz kraken
#

a struct is like a class

#

except its not got an actual object attached to it

#

any and all methods it has are static

#
struct MyStruct
{
  int aValue;
  int anotherValue;
}```
sharp sun
#

so would I just be able to add that under the public class, and place my variables inside of it?

topaz kraken
#

you could

#

but

#
public struct PlayerMovementData
{
// . . .
}
public class PlayerMovement : MonoBehaviour
{
//. . .
}
#

they can also replace classes entirely

sharp sun
#

oh?

topaz kraken
#

it doesnt do anything, its not an object, its RAW data

sharp sun
#

that seems like it would have a lot of potential tbh

topaz kraken
#

a pure data structure which can be accessed anywhere pretty much

#
public struct PlayerMovementData
{
// . . .
}
public class PlayerMovement : MonoBehaviour
{
  PlayerMovementData MyPlayerMovementData;
}
#

you can create a whole .cs file for a struct if need be

#

its worth having a look on yt for info on them

#

they are insanely powerful

sharp sun
#

So if I wanted the datasets for all of the classes (5)

#

i would just put a struct above the public class with the data

topaz kraken
#

you dont pass a reference to a struct by default, its always copied

sharp sun
#

and then call it down?

sharp sun
#

where would the position be?

topaz kraken
#

in its own file, i put them one above the other to show that the scope is the same as a class

sharp sun
#

Ohh

#

so i would need a new script

topaz kraken
#

yeah i guess so, though once you delete : MonoBehaviour and replace class with struct it is no longer a script, its just a file with some data

#

heres a good example of a struct

sharp sun
#

alright so if I’m following correctly:
I make a C# script in unity
Make a struct
Fill out with the data of each class
Then use that in PlayerMovement?

topaz kraken
#
struct Coordinate
{
    public int x;
    public int y;
}```
#

you can create it with static variables if need be

#

this is the approach you are likely going to need

#

statics just exist for everything with access to that file, as if they were carved in stone

#
struct playerSettings
{
  public static float PlayerSpeed = 10f;
}```
sharp sun
#

yeah, I would be able to reference it anywhere

#

but how would I change the variable?

topaz kraken
#

you are also able to make structs that dont have a value already, youve likely used them in other places

sharp sun
#

Not through like clicking and typing a different number

topaz kraken
#

youve probably not even noticed

#

hm

sharp sun
#

but how would I be able to make the speed of one character different to the speed of another

topaz kraken
#

here

#
struct Coordinate
{
    public int x;
    public int y;

    public Coordinate(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}```
#

now in any script you can do this

#
int myX = 10;
int myY = 10;
Coordinate MyCoordinate = new Coordinate(myX, myY);
#

this way you can share the type called Coordinate between any file

sharp sun
#

I’m not following 😓

topaz kraken
#

so for instance, each character would need a PlayerSettings

#

but you can also have another file which stores ALL settings

sharp sun
#

alr

#

that makes sense

#

its more efficient

#

to just have one

topaz kraken
#

its all part of the learning process, just figuring out what you like tbh

sharp sun
#

yeah

topaz kraken
#

its never a bad thing though to just have a monoBehaviour with some floats on it attached to a gameObject

#

at least you get a nice menu

#

its all up to you on that front

sharp sun
#

alr so I have a question

topaz kraken
#

yeah whats up?

sharp sun
#

if I used a static with the datatables

#

Would I need to make a whole new variable for each character

#

like

#

if I wanted just a general speed variable that changed with each character, how would that be accomplished??

topaz kraken
#

if you made a struct with static, hard coded values, just pretend that unity reads that file like you do

sharp sun
#

yeah so each character/class has a different speed

topaz kraken
#

do you mean they all share speed? or they all share behaviour and have their own speed

sharp sun
#

ok I think we’re on

#

different pages

topaz kraken
#

the monoBehavior isnt static

sharp sun
#

Alr may I explain some stuff to make sure we’re on the same page

topaz kraken
#

if you drag that script onto 5 different gameObjects, you can set it up in 5 different ways without it conflicting

#

like i can make a script for jump, attach it to 3 characters, and give them individual jump heights just by changing the value in the inspector

#

per gameObject

sharp sun
#

well they’re all the same gameobject

#

here let me explain

#

fully

topaz kraken
#

are they though?

#

its a class

#

a class means classification, a little factory that creates an object of its own

#

by using it in 5 different places, you get 5 different object instances in memory

sharp sun
#

I want a class system with 5 roles that have different and individual stats like speed, jumpingPower, etc.
The Player uses the same gameobject with each role.

#

so how would I make this one GameObject change its data

#

when you use a different role

topaz kraken
#

lets say role or profession instead of class, its a little confusing lol

sharp sun
#

also I don’t mean Class as in unity class

#

I really should’ve specified

topaz kraken
#

okay so we can make a Role as an actual file

#

or at least the factory that describes what a role is and lets you create one

sharp sun
#

I’m just using a gameobject with a player controller that changes its sprite with a script

topaz kraken
#
struct Role
{
  float speed;
  float weight;
  float jumpHeight;
}```
sharp sun
#

Alr

#

lemme type this in

topaz kraken
#

not exactly, but something similar, right?

sharp sun
#

Yes

#

I don’t use a weight system

#

but the other two are there

#

Alright I have that typed in

#

What do I do next

topaz kraken
#

well your also going to need a character as well, so your character can have its own Role

#

this may have been the perfect spot for the scriptable object, like i say, this is entirely down to how you want to build your game

sharp sun
#

the CharacterManager has a variable that tells everything which role you’re currently playing as

#

would that work?

#

oh jesus I’ve been here for a while

topaz kraken
#

maybe? im not entirely sure on the full architecture of the project

sharp sun
#

so if I just have a struct that has each variable

#

and it knows which character it is

#

how do I make it change the variables

#

Gods i feel like a broken record XD

topaz kraken
#

character.speed = myArcherStruct.speed

#

myArcherStruct is just its own Role

#

Role MyArcher = new Role(define variables here)

#

the manager could have some scripts on it that lets you tweak those values if you want to approach it like that

sharp sun
#
using UnityEngine;

struct Character
{
    float speed;
    float jumpHeight;
}
topaz kraken
#

role

sharp sun
#

yeah I just replaced it for my own access

#

here

topaz kraken
#

its not the character itself, just the values a role can actually modify

sharp sun
#

yea

#

Alr

#

changed

#

ok so I have that

topaz kraken
#

is "using unityEngine" greyed out for you in the text editor?

sharp sun
#

No

topaz kraken
#

huh

sharp sun
#

I’m using Visual Studio btw

#

“using” is blue

#

and “UnityEngine” is green

topaz kraken
#

strange, never would have expected a struct to require the unityengine

#

its just raw data

sharp sun
#

Do i just delete Using UnityEngine?

#

it says there’s no issues

topaz kraken
#

then yeah

#

if theres no issues

sharp sun
#

ok so how do the variables change

topaz kraken
#

youre creating a type

sharp sun
#

yes

topaz kraken
#

just like a vector3

sharp sun
#

ok..

topaz kraken
#

so a role is no different to a Vector3 really, you can set its values in code wherever and whenever you please

sharp sun
#

ok so

topaz kraken
#

in a script, you can now do Role Runner = new Role();

#

Runner.Speed = something

sharp sun
#

in my PlayerMovement Script?

topaz kraken
#

no the playermovement should only move the player

#

nothing else

sharp sun
#

ok

#

make a new script then

topaz kraken
#

if you have a script that manages your character roles, use that maybe

sharp sun
#

alr I do

topaz kraken
#

then you can go ham on declaring as many roles as you like

sharp sun
#

so ill put it in that

topaz kraken
#

then each character can ask the manager file for the role it needs

sharp sun
#

where would I add it?

#

Like position wise

#

Just anywhere?

topaz kraken
#

global scope

sharp sun
#

Ok alr

#

so like under the public class?

#

or outside of everything

topaz kraken
#
class RoleManager : MonoBehavior
{
  Role RunnerRole;
  Role JumperRole;
  Role ChefRole;
}```
#

thats global scope

#

any method can access it

sharp sun
#

Alr I did it for all the roles

#

sorry

#

not classes

#

i forgot

topaz kraken
#

now you can use the RoleManager file to make a little menu in the inspector that lets you pick each value for each role

sharp sun
#

where’s the role manager file?

sharp sun
#

Ohh

#

So the role manager script

topaz kraken
#

you know this is all ideas on how you could solve the problem right?

sharp sun
#

what?

topaz kraken
#

youve got to architect and figure out a neat solution, im just explaining how you can hook the things together

sharp sun
#

yeah ofc

topaz kraken
#

okay so RunnerRole.Speed = someValue

sharp sun
#

oh wait

#

add that to the role manager script?

topaz kraken
#

you can now have the character ask roleManager for RunnerRole.Speed

#

the RoleManager can have public variables for each speed

#

thats all there is to the roles

sharp sun
#

that works perfectly for me

#

but

#

where do I add that

#

last tidbit

topaz kraken
#

in the roleManager

#

maybe the start method

sharp sun
#

Under the start method?

#

hold on let me

#

read all of this

topaz kraken
#
class RoleManager : MonoBehavior
{
  public Role RunnerRole;
  public float RunnerSpeed;
  public float RunnerJumpHeight;

  public Role JumperRole;
  public float RunnerSpeed;
  public float RunnerJumpHeight;

  public Role ChefRole;
  public float RunnerSpeed;
  public float RunnerJumpHeight;

  void Start()
  {
    RunnerRole.Speed = RunnerSpeed;
    etc...
  }
}
sharp sun
#

Thank you

topaz kraken
#

now a character can have a reference to the RoleManager and ask it to provide RunnerRole.Speed

#

its not the cleanest way around things, but it should make things clear enough that rebuilding or redesigning the solution wont be such a pain down the line

#

but, for a few hours in, were not building google

sharp sun
#

yeah, ofc

#

this is kind of a small project XD

#

I’m pretty sure just about every article in the world says never to start with a big unity project

topaz kraken
#

im doing something similar, i did have a character controller built, but im breaking it all up into tiny scripts that handle JUST the things they are asked to

#

its a good convention to name behaviours as behaviours

#

ill get an example of my stuff up

sharp sun
#

Cool!

topaz kraken
#

move state manager is interesting, it chooses which movement script to use depending on what the character is up to

#

those scripts can tell the behaviour scripts when or if to behave

#

or how

#

the idea is, when grounded, the state becomes this

#

these scripts handle each state

sharp sun
#

Ohh so it uses multiple smaller scripts

topaz kraken
#

its always a good idea to break things down into small components, rather than handling everything in one place

#

yes, and it avoids me nesting "if if if"

#

which also avoids making bugs super easy

#

when im in the air, the ground detector naturally fails to find ground, it reports to my MoveStateManager that there is no ground

sharp sun
#

wait how does it detect the difference between Air and Jump states?

topaz kraken
#

the MoveStateManager then switches to AirBorne and relies on MoveStateAir for its Update and FixedUpdate Methods

#
public class MoveStateAir : MoveBaseState
{
  public override string Name
  {
    get { return "Move State Airborne"; }
  }

  public override void EnterState(PlayerMoveStateManager context)
  {
    Debug.Log("Move State Entered: Air");
    base.EnterState(context);
  }

  public override void ExitState()
  {
    Debug.Log("Move State Exited: Air");
  }

  public override void UpdateMovement()
  {
    if (GroundDetectBehaviour.GetGroundState() == GroundState.air)
    {
      if (!GravityBehaviour.GravityEnabled)
      { 
        GravityBehaviour.EnableGravity(Context);
      }
    }
    else
    { 
      Context.SwitchState(Context.GroundedState);
    }
  }
}```
#

the air state just asks the ground detector if its grounded, if it is, then it asks context (the manager upstairs) to switch to ground state instead

sharp sun
#

damn dude

topaz kraken
#

and the reverse applies for not being grounded anymore, switching to airborne

sharp sun
#

thats really good ngl

#

how long have you been programming for?

topaz kraken
#

on and off for about a decade

sharp sun
#

wow

topaz kraken
#

im not a wizard by any means, this code is simple so I dont get confused

sharp sun
#

that’s about a decade longer than I have

#

XD

topaz kraken
#

this is a cool thing you can do with object oriented code tho

#
using UnityEngine;

public abstract class MoveBaseState
{
  public abstract string Name
  {
    get;
  }

  private PlayerMoveStateManager _context;
  public PlayerMoveStateManager Context
  {
    get { return _context; }
  }

  // Behaviours
  public GroundDetectBehaviour GroundDetectBehaviour
  {
    get { return _context.GroundDetectBehaviour; }
  }
  public GravityBehaviour GravityBehaviour
  {
    get { return _context.GravityBehaviour; }
  }

  public virtual void EnterState(PlayerMoveStateManager context)
  {
    _context = context;

  }

  public abstract void ExitState();
  public abstract void UpdateMovement();
}```
#

you can describe an abstract class

#

its essentially a blueprint that forces other classes to be like itself

sharp sun
#

whats an Abstract class?

topaz kraken
#

if it doesnt fit the blueprint, visual studio AND unity lose their shit

#

like when you say MyClass : MonoBehaviour

#

well here, im doing the same

#

MyMovementState : MoveBaseState

#

virtual voids MUST be replaced

#

abstract voids CAN be replaced

#

heres where it gets similar to your role system

#
public class PlayerMoveStateManager : MonoBehaviour
{
  public string currentStateName;
  public MoveBaseState currentState;
  public MoveStateIdle GroundedState = new MoveStateIdle();
  public MoveStateJump JumpState = new MoveStateJump();
  public MoveStateWalk WalkState = new MoveStateWalk();
  public MoveStateAir AirState = new MoveStateAir();
...
#
...
 private void FixedUpdate()
  {
    currentState.UpdateMovement();
  }

  public void SwitchState(MoveBaseState state)
  {
    currentState = state;
    currentState.EnterState(context: this);

    currentStateName = currentState.Name;
  }
#

see how my SwitchState wants the MoveBaseState?

#

i can give it a JumpState, or a GroundedState

#

they count as MoveBaseStates

sharp sun
#

ohhhhh

#

that’s actually really cool

#

kind of a generalizer of sorts

topaz kraken
#

mine are functional, rather than data, thats the main difference#

#

yeah, its a state machine

#

each state is a file, each one has its own version of UpdateMovement

sharp sun
#

Also if I may ask a question

#

about my code

topaz kraken
#

and each one has a little function that runs once when its been switched to

topaz kraken
sharp sun
#

where do I input the values for each character?

#

Like here ill send a screenshot

topaz kraken
#

make sure the tanker is public for a start

#

now, make a file for character

sharp sun
#

Oops

#

yeah

sharp sun
#

wdym file?

topaz kraken
#

or use the movement script you have

sharp sun
#

Oh ok

topaz kraken
#

make a gameObject reference, like "public GameObject MyRoleManager"

#

then in the unity editor, drop the role manager object on the character

#

now, in any way you like, have the movement or character script, whatever you decided to use, go like "speed = MyRoleManager.RunnerRole.Speed"

#

make sense?

#

youve now got a place where all the role values are stored, and can be accessed by any other gameobject

sharp sun
#

ok so

#

uh

topaz kraken
#

like

if (MyCharacterIsARunner)
{
  //set your speed here or something
}```
sharp sun
#

Ohhhh

#

Ok

#

Thank you

#

and do i give that global range?

topaz kraken
#

you cant make an if global as its functionality

#

you can do that inside a method

sharp sun
#

which method would I do

topaz kraken
#

like start, or update, if you are able to change roles mid game

sharp sun
#

alr

#

I don’t think I’ll have that feature atm

topaz kraken
#

or make a new one called "ChangeRole(Role MyChosenRole)"

#

actually thats probably the best way to do it

sharp sun
#

wait so what

#

what method do I place this under?

#

if I don’t want changeable roles mid game

topaz kraken
#
void ChangeRole(Role ChosenRole)
{
  Speed = ChosenRole.speed;
  JumpHeight = ChosenRole.JumpHeight;
}```
#

its own method

sharp sun
#

Ohh ok

topaz kraken
#

then in start you can hard code the role you want for now and worry about a decent way to switch later on

#
void Start()
{
  ChangeRole(RoleManager.RunnerRole);
}```
#

so when the script starts, it runs the changeRole function, it asks the RoleManager we made a reference to for the values from RunnerRole, then ChangeRole sets your characters Speed and Jump Height to those values

sharp sun
#

ok that makes sense

topaz kraken
#

the RoleManager can just be an empty gameobject in the scene at 0,0

#

i do the same thing for input too

sharp sun
#

is the role manager supposed to be a gameobject?

#

i just have a script

topaz kraken
#

yeah

#

see how i have this

sharp sun
#

I should just be able to drag it into the hierarchy tho right?

topaz kraken
#

not entirely

#

create empty

sharp sun
#

K

topaz kraken
#

its just a position with nothing else on it

sharp sun
#

hold on

#

Ok

#

done

topaz kraken
#

thats the unity editor version of global

#

an empty gameobject

sharp sun
#

Ohh

#

ok

#

now what do I do with it

topaz kraken
#

it has no hitbox, no physics, nothing, just a position value, which is a good idea to set to 0,0,0

#

now add your RoleManager script to it

sharp sun
#

Done

topaz kraken
#

now your character, it will want a reference to that object

sharp sun
#

Ok

#

how do I add it

#

like

#

the reference

topaz kraken
#

like this

sharp sun
#

Yeah but

#

where

#

ik you can add references

topaz kraken
#

that is the reference

sharp sun
#

Nonono

#

which box do I place the reference in

topaz kraken
#

your character

#

it will want a reference to the role manager

sharp sun
#

I understand

#

that

topaz kraken
#

so, like my player wants a reference to the inputManager

sharp sun
#

I understand references

#

I don’t know which box

topaz kraken
#

did you make one?

sharp sun
#

make what

topaz kraken
#

in your character public RoleManager MyRoleManager

sharp sun
#

in my character script?

topaz kraken
#

yep

#

so that it can talk to the role manager

sharp sun
#

Ok thank you

#

and no i dont think i did

#

I have
public GameObject MyRoleManager;

topaz kraken
#

oh yeah that lol

#

now save the script, go back to unity, and drag the RoleManager Empty we made earlier and drop it onto the box that shouldve appeared on the character's movement script

sharp sun
#

ok so

#

I don’t see any box

topaz kraken
#

public RoleManager MyRoleManager

#

none that correspond to this? ^

sharp sun
#

was I supposed to add that?

#

I did that and I still dont see a box

#

im sorry for being such a hassle my mans

#

im trying to be quick but I’m not that great at this

topaz kraken
#

see here?

#

in my PlayerMovementStateManager, i have a line that says public GameObject InputManager

#

it makes the box appear, then i go into unity and drop a gameobject, which ive aptly named InputManager

sharp sun
#

yeah

#

but my box

#

isnt there

topaz kraken
#

go back to VS and hit Save all

sharp sun
#

here look

#

I showed you

#

Oh shit dude I have like 5 compiler errors

topaz kraken
#

was that public GameObject MyRoleManager in the player movement file?

sharp sun
#

Yeah

topaz kraken
#

huh

sharp sun
#

i have two

topaz kraken
#

whats the compiler errors?

sharp sun
#

i have the gameobject one

topaz kraken
#

wait why two?

sharp sun
#

and the public RoleManager one

topaz kraken
#

oh delete the one thats not the gameobject that was communication error

sharp sun
#

Ohhh

#

Ok

topaz kraken
#

the compiler was probably like "what the hell is a RoleManager"

sharp sun
#

Yep

#

Also

#

its still

#

not there

topaz kraken
#

ight save all and it should make the box

#

oh, any more compiler errors?

sharp sun
#

Yeah

topaz kraken
#

it wont update in unity til all of them are gone

#

lets see them

sharp sun
topaz kraken
#

is the class named skinmanager a public class?

#

or just a class

sharp sun
#

Yes

#

public

topaz kraken
#

double click the error

sharp sun
#

Im alr in SkinManager

topaz kraken
#

is Role public?

sharp sun
#

yes

#

do you want a screenshot of the code?

topaz kraken
#

is speedster public

sharp sun
#

yes

topaz kraken
sharp sun
#

wait

#

wdym

topaz kraken
#

something says it isnt

sharp sun
#

where’s “Speedster”

topaz kraken
#

double click it so it takes you to the exact line where it dies, screenshot it and lemme see

sharp sun
#

Ok

topaz kraken
#

i found it in your debugging console lol

sharp sun
#

Nonono

#

I get that

topaz kraken
#

but that is the error

#

thats all

sharp sun
#

nvm

#

nvm

topaz kraken
#

fixed it?

sharp sun
#

?

#

no

#

I haven’t

topaz kraken
#

theres no red squiggly lines?

sharp sun
#

I don’t see any

topaz kraken
#

any errors in VS?

sharp sun
#

tbf i’ve never seen any

#

nope

topaz kraken
#

thats the place to check for code issues

#

okay in unity, do this

sharp sun
#

Clicked it

#

still there

topaz kraken
#

when i asked if role was public i meant this

#
public struct Role
{
//...
}```
#

make the whole file public

#

should resolve it

sharp sun
#

wait where are we putting struct?

topaz kraken
#

shouldve probably been clearer lol

sharp sun
#

Wait

#

nvm

#

Sorry

topaz kraken
#

the role file we made ages ago

sharp sun
#

Yea

topaz kraken
#

yeah its probably the issue, should fix things

sharp sun
#

and also

#

no it wasnt

topaz kraken
#

it wasnt public?

#

or wasnt the issue?

sharp sun
#

You’re not gonna believe this

#

its working

sharp sun
#

And now i have more errors

#

jesus

topaz kraken
#

oh i believe it, i just asked and took "yes its public" for an answer and was stumped til i realized you probably meant a reference to it lol

#

those errors btw, they are super common phrases, dont be shy at just typing the whole error into google

sharp sun
topaz kraken
#

99% of the time it just fixes everything

sharp sun
#

H elp me

#

theres

#

so many

topaz kraken
#

theres only a couple but on repeat

sharp sun
#

True

topaz kraken
#

bc you have multiple roles ofc

sharp sun
#

So whats causing this

topaz kraken
#

check in VS not in unity

sharp sun
#

I am

topaz kraken
#

its error list is way more powerful

sharp sun
#

wheres that?

topaz kraken
#

the big box at the bottom

#

the red X is the error

sharp sun
#

I don’t see it

topaz kraken
#

"does not exist in the current context" means you dont even have a variable with the name

#

like you just didnt make it, but you are asking for data from it

#

is that visual studio, or visual studio code?

sharp sun
#

how do I tell the difference?

#

It says Visual Studio 2022

topaz kraken
#

okay thats VS

#

VSCode is its own special thing and doesnt come with much help for unity

sharp sun
#

oh

#

so do I need that?

#

or not

topaz kraken
#

are you in the file that its complaining about

sharp sun
#

i am

topaz kraken
#

no i was just checking you had the right thing installed

sharp sun
#

oh ok

topaz kraken
#

just search error list and click it

sharp sun
#

Ohh

#

ok

#

Yeah its empty

topaz kraken
#

in unity, double click one of those context related errors then

#

see where it takes you

sharp sun
#

PlayerMovement

topaz kraken
#

to the exact line

sharp sun
#

i was in the wrong one 😑

topaz kraken
#

it doesnt just open a file, it opens to a line

sharp sun
#

ok hold on

topaz kraken
#

it puts your caret on the bug itself

sharp sun
#

Yeah

#

i see it

topaz kraken
#

this appeared?

sharp sun
#

No

topaz kraken
#

then unity is talking crap

sharp sun
#

Heres

#

everything

topaz kraken
#

clear the error list in unity again

sharp sun
#

Alris

#

Nope

#

its all still there

topaz kraken
#

is the game running?

sharp sun
#

Hold on

#

Let me try something

topaz kraken
#

save ALL files and reboot bot unity and VS bc this shouldnt be happening unless something is talking crap

#

if these are new errors, lets see them

sharp sun
#

Alright

#

ill reboot unity

topaz kraken
#

screen first, lets see if we cant make sense of why unity is throwing a fuss over nothing

sharp sun
#

Its alr rebooted

#

sorry

topaz kraken
#

oof

sharp sun
#

its booting up

#

right now

#

jesus man i thought this was gonna be a lot easier

#

😅

topaz kraken
#

this is just how software dev is

sharp sun
#

how fun

topaz kraken
#

over time you will get to know what each error really means and how to hunt them down super quick

sharp sun
#

hopefully lol

topaz kraken
#

when i see "doesnt exist in the current context" i always think "yep, i know, i didnt write float x; at the top of the file, thats why x is crying"

sharp sun
#

Ok so i have different variable names for speed and jumpheight

#

Im just gonna match them all up

topaz kraken
#

change the ones at the top of the file to match the ones i wrote

sharp sun
#

?

topaz kraken
#

public stuff should always BeLikeThis and private stuff should beLikeThis

sharp sun
#

yeah I did

#

Oh

#

Ok

topaz kraken
#

and local scope private stuff should _beLikeThis

sharp sun
#

Ok do you need a screenshot or anything

topaz kraken
#

thats not a code error, thats just a bit of grammar so you know what you are reading without having to scroll all the time

#
    SprintHeld = playerControls.Default.Sprint.IsPressed();
    JumpPressed = playerControls.Default.Jump.WasPressedThisFrame();
    CrouchPressed = playerControls.Default.Crouch.WasPressedThisFrame();
    CrouchReleased = playerControls.Default.Crouch.WasReleasedThisFrame();

    Vector2 _movement = playerControls.Default.Move.ReadValue<Vector2>();
    Vector2 _padLook = playerControls.Default.GamepadLook.ReadValue<Vector2>();
    Vector2 _mouseLook = playerControls.Default.MouseLook.ReadValue<Vector2>();

    if (_movement.magnitude > LeftStickDeadzone)
      MoveInput = _movement.normalized;
    
    if (_padLook.magnitude > RightStickDeadzone)
      LookInput = (_padLook * new Vector2(StickSpeedHorizontal, StickSpeedVertical)).normalized;
    else
      LookInput = _mouseLook * new Vector2(MouseSpeedHorizontal, MouseSpeedVertical);```
#

like this stuff, i declared _movement inside my update function, so its lowercase and has an underscore

sharp sun
#

Yeah ok so everythings matching

topaz kraken
#

MouseSpeedVertical is a public global

sharp sun
#

I think

#

i know why though

#

i removed the original speed and jumpheight variables

#

let me

#

readd them

topaz kraken
#

yup i thought that was the case, i was trying to see if i could get you to find your own missing variables

#

i dont wanna just say "its this"

sharp sun
#

yeah, helps me actually learn

topaz kraken
#

id rather show you how to debug something and understand the error in front of you

sharp sun
#

thank you man

topaz kraken
#

np

sharp sun
#

Most people wouldn’t even put in that much effort

#

would you mind if I friend requested you?

topaz kraken
#

im just sat with tunes on drinking a coffee

sharp sun
#

not to weird lol

topaz kraken
#

i mean if you like, sure

sharp sun
#

Ok so I added those variables

topaz kraken
#

yup, now clear the errors in unity and see whats going on there

#

see whats left

sharp sun
#

A lot of them are gone

topaz kraken
#

what are the leftovers looking like?

sharp sun
#

i think I can solve one

#

here lemme screenshot

topaz kraken
#

good idea

sharp sun
topaz kraken
#

okay easy, all of the "does not exist in the curren context" issues are identical to the one weve just solved

sharp sun
#

Yeah I think

#

i can fix them all

topaz kraken
#

for all the current context ones, just double click and then create that variable globally

sharp sun
#

ok

#

i solved all

#

of the does not exist in current context ones

topaz kraken
#

and the Role.JumpHeight

sharp sun
#

It was because I didn’t have them all matched up

topaz kraken
#

it has an issue with access levels

#

so something is public and something else isnt, and its causing a conflict

sharp sun
#

Ohhh

topaz kraken
#

so make all the variables inside that struct file public

sharp sun
#

Ok

topaz kraken
#

it also looks like its saying the struct doesnt have a variable called speed

sharp sun
#

The one with like

#

no code

#

Or sorry

topaz kraken
#

whats the struct file look like now

sharp sun
#

Very little code

#

Here

#

Ss

#

Thats it

topaz kraken
#

capitalize them both to match the rest of the files

#

since they are public

sharp sun
#

i matched everything

#

Annnddd

#

No more errors

topaz kraken
#

Speed
JumpHeight

#

AY

#

nailed it

sharp sun
#

oh should they be

#

capitalized

#

let me do that

topaz kraken
#

hold up

sharp sun
#

Yeah?

topaz kraken
#

if they are being referenced in more files than just that one, which they are, theres a better way]

#

right click the name and rename

#

then type it with the capital

#

it will apply that name across any file that references it, so no bugs appear

sharp sun
#

I don’t see that button

topaz kraken
#

double click the word speed so its selected

#

then CTRL+R

sharp sun
#

Yes

#

Yep

topaz kraken
#

gone green?

sharp sun
#

no

#

its still blue

topaz kraken
#

might have to do it twice lol

#

VS has some complex keybinds

sharp sun
#

“the key combination (Ctrl + R, Ctrl + R) is bound to command (.Refactor.Rename) which is not currently available.”

#

thats what it says

topaz kraken
#

weird

sharp sun
#

Its fine

#

I can use the find/replace tool

topaz kraken
#

oof

#

maybe try doing it in another file

#

save all first

sharp sun
#

I’m done

#

did it with f/r

topaz kraken
#

oh its all up to date then?

#

no errors in unity?

sharp sun
#

nope

topaz kraken
#

a side thing for organizing code btw

#
  #region Input Settings
  [Header("Stick Settings")]
  [Range(0, 1)] public float LeftStickDeadzone;
  [Range(0, 1)] public float RightStickDeadzone;

  [Header("Stick-Look Settings")]
  public float StickSpeedHorizontal;
  public float StickSpeedVertical;

  [Header("Mouse-Look Settings")]
  public float MouseSpeedHorizontal;
  public float MouseSpeedVertical;
  #endregion

  #region Accessors
  public Vector2 MoveInput
  {
    get; private set;
  }
  public Vector2 LookInput
  {
    get; private set;
  }
  public bool SprintHeld
  {
    get; private set;
  }
  public bool JumpPressed
  {
    get; private set;
  }
  public bool CrouchPressed
  {
    get; private set;
  }
  public bool CrouchReleased
  {
    get; private set;
  }
  #endregion```
#

use #region NAME and #endregion

sharp sun
#

So now

#

where do I input values

#

to make them different

topaz kraken
#

in unity

sharp sun
#

Oh yeah

topaz kraken
#

click on the RoleManager

sharp sun
#

Drop in role manager

#

right?

topaz kraken
#

yeah

sharp sun
#

Ohhhh

#

I SEE IT!!

#

thats so cool

topaz kraken
#

the roleManager is where you dun dun dun, manage your roles

topaz kraken
#

this is the pro tip i was showing

#

you can make regions and fold them away

sharp sun
#

let me check if it works rq

#

And I cannot move

topaz kraken
#

did you actually made a speed for the role you want

sharp sun
#

oh i see why

#

my player has the role manager

#

in him

topaz kraken
#

lol

sharp sun
#

Or the script at least

topaz kraken
#

yeah remove component on that

sharp sun
#

is it ok to get rid of that?

#

ok

topaz kraken
#

the role manager object is global so that any character can talk to its role manager script from the scene

sharp sun
#

Uhhh

#

I still cant move

#

Let me check the movement script

topaz kraken
#

yeah, its likely youve got the move speed data working fine now

#

but also not moving the player at all

sharp sun
#

i can still switch directions

#

its just

#

the player

#

isnt moving at all

topaz kraken
#

if you make the move speed on the player public, you can see what its value is in the inspector and see if its getting 0

#

this is how i usually debug

#

temporarily make things public and see what they do

sharp sun
#

Oki

#

Ill check

#

Yeah its 0

topaz kraken
#

well, in the role manager, what are the speeds set to

#

your character has a role picked, right?

#

lets say, runner

sharp sun
#

they’re all set to numbers

#

Wait

#

if its a float variable

#

do i need to add the F

topaz kraken
#

nope

sharp sun
#

at the end in the text box thing

#

Ok

topaz kraken
#

thats only if im doing something like x += 0.5f

sharp sun
#

Here let me send you the code

topaz kraken
#

so it knows im adding a float to a number

#

ok

sharp sun
topaz kraken
#

thats not the issue

#

its doing that perfectly

sharp sun
#

alr

#

then why isn’t it moving?

topaz kraken
#

speed is 0

sharp sun
#

oh

#

ok

topaz kraken
#

we need to know why

#

in the player movement script, where do you set the speed

sharp sun
#

Its under
public class PlayerMovement : MonoBehaviour

topaz kraken
#

what line

sharp sun
#

17

topaz kraken
#

gotta know how its getting its value of 0

#

i mean whats the line say

sharp sun
#

im trying to screenshot

#

its not working

#

Wait nvm

#

I got it

topaz kraken
#

copy paste the code into here

sharp sun
#

Not the error

#

the screenshot

#

Thats what determines it

#

its white

#

that’s probably bad

topaz kraken
#

no thats fine

sharp sun
#

wait

#

Dont i need

#

to add public float

topaz kraken
#

this is white too and is probably one of my most crucual things

topaz kraken
#

Speed is a float already

sharp sun
#

in the front of Speed and JumpHeight

#

Oh

#

yeah

topaz kraken
#

no

sharp sun
#

true

topaz kraken
#

thats already declared globally

sharp sun
#

this

#

is just setting it

topaz kraken
#

yup

sharp sun
#

so then

#

Why isn’t it setting it

topaz kraken
#

it probably is

sharp sun
#

But it says it isn’t

topaz kraken
#

add a Debug.Log(ChosenRole.Speed); to that void

sharp sun
#

Ok

#

Then check console right?

topaz kraken
#

yeah run it and see what it says

#

im betting it says 0

sharp sun
#

It doesn’t say anything

topaz kraken
#

so you arent actually running that method at all

sharp sun
#

yeah I don’t think so

#

so how do i

topaz kraken
#

what about the start method

sharp sun
#

run the method

#

what start method?

#

So do i just

#

replace the first chosenrole

#

with start?

topaz kraken
#

no

#
void Start()
{
  ChangeRole();
}```
#

you need a start method

sharp sun
#

Ok so

topaz kraken
#

so when the game starts, this method kicks off

#

you will need to pass in the role you want to start with as well

sharp sun
#

What do I put where it says “ChangeRole();”

topaz kraken
#
void Start()
{
  ChangeRole(RoleManager.RunnerRole);
}```
sharp sun
#

Oh am I writing this

topaz kraken
#

when the game starts, the character changes its role to the RoleManager's Runner Role

#

or whatever role you want it to start with

sharp sun
#

so like this?

topaz kraken
#

yeah pretty much thats it

#

the Start method runs once when the object its on, the character, is loaded

sharp sun
#

ok let me do it

#

Ok more errors 😁

#

hahhaha

#

unity just loves them dont they

topaz kraken
#

yup

#

what are they?

sharp sun
topaz kraken
#

ChangeRole

#

change to the chosen

sharp sun
#

Oh ok

#

that worked

#

thanks

#

What about the other one

topaz kraken
#

double click it

sharp sun
#

Okie dokie

#

I’m in

topaz kraken
#

wheres it taken you?

sharp sun
#

Right here

topaz kraken
#

hm

sharp sun
#

Wait btw

#

why is it Speedster

#

or was runner just an example

topaz kraken
#

try changing the void ChangeRole(Role ChosenRole) to static void ChangeRole(Role ChosenRole)

#

they were examples

#

you can change the roles at any time, they are generic names for a struct now