#Monodeath's Code Error

1 messages · Page 2 of 1

sharp sun
#

So change Speedster to ChosenRole

topaz kraken
#

no

sharp sun
#

Or?

#

what do i put there then

topaz kraken
#

the void under it

#

make it static

sharp sun
#

Ok

topaz kraken
#

static void

#

see what happens, it might still complain]

sharp sun
#

But wait

#

why is it speedster?

#

here let me show you

topaz kraken
#

you chose that didnt you?

#

we made it up

sharp sun
#

Yeah but what’s supposed to be there

topaz kraken
#

anything that is a role

sharp sun
#

Ohhh

#

Ok

#

So it doesn’t matter

topaz kraken
#

yeah no, the error says An object reference is required for the nonstatic field, method, or property

sharp sun
#

Oh okay

topaz kraken
#

something isnt static, or we need an object reference

#

so, hows the void looking, static?

sharp sun
#

not good

#

many errors

topaz kraken
#

undo that then

sharp sun
#

alr

#

Original error is still there

#

Do you want to see the code or anything?

topaz kraken
#

oh wait

#

you are asking for the roleManager script to give you its value

sharp sun
#

yeah

topaz kraken
#

not the object reference for its script's value

sharp sun
#

yes

topaz kraken
#

yes

sharp sun
#

yes

#

So how do i fix it

topaz kraken
#

put the whole movement class in here, as text

#

the screenshots are mega confusing me atm

sharp sun
#

Oh

#

sorry

topaz kraken
#

im working off memory of bits

sharp sun
#

Gimme the template rq

topaz kraken
#

then i can edit it to fix the thing

sharp sun
#

actually nvm

#

''' csharp

#

Oops

#

didn’t mean

#

To press enter

topaz kraken
#

reverse apostraphe

#

`

#

left of 1

#

`123

sharp sun
#

I see it

topaz kraken
#

no spaces before csharp

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

public class PlayerMovement : MonoBehaviour
{
    private float horizontal;
    public float Speed;
    private float JumpHeight;
    private bool isFacingRight = true;
    public GameObject MyRoleManager;

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

    void Start()
    {

    ChangeRole(RoleManager.Speedster);
    }

    void ChangeRole(Role ChosenRole)
    {
        Speed = ChosenRole.Speed;
        JumpHeight = ChosenRole.JumpHeight;
        Debug.Log(ChosenRole.Speed);
    }

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

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

        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()
    {
        rb.velocity = new Vector2(horizontal * 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
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private float horizontal;
    public float Speed;
    private float JumpHeight;
    private bool isFacingRight = true;
    public GameObject MyRoleManager;

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

    void Start()
    {
      // change the role to MyRoleManager's Script's Variable named Speedster
      ChangeRole(MyRoleManager.GetComponent<RoleManager>().Speedster);
    }

    void ChangeRole(Role ChosenRole)
    {
        Speed = ChosenRole.Speed;
        JumpHeight = ChosenRole.JumpHeight;
        Debug.Log(ChosenRole.Speed);
    }

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

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

        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()
    {
        rb.velocity = new Vector2(horizontal * 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;
        }
    }
}```
#

we were asking the RoleManager script for speedster

#

the script is static, because its a file

#

we needed an instance of it

sharp sun
#

Do i just copy paste that?

topaz kraken
#

yes

sharp sun
#

Ok

#

copied

topaz kraken
#

but yeah, we needed an instance of the script, so we get the MyRoleManager (the object from the scene) and ask for its current version of the script, then ask that for the speedster

sharp sun
#

It works!

topaz kraken
#

ay, and the code is clean too

sharp sun
#

awesome!

topaz kraken
#

cant hurt to maybe refactor a little bit though, but for now its ok

sharp sun
#

so movement should work now right?

topaz kraken
#

should yeah

#

try

sharp sun
#

It does!

#

And now

#

let me see if different roles work

topaz kraken
#

now try changing from speedster to another role and seeing if movement is different, but still working

#

beat me to it

#

you know, if you made an NPC spawner you could randomly assign roles to them too

sharp sun
#

Uhhh

#

okay so

#

I don’t think its working

#

but

#

i think I know why

topaz kraken
#

the roles arent set properly?

sharp sun
#

The rolemanager doesnt use words

#

it uses numbers

#

so if i just make it convert it to string var

#

it should work

topaz kraken
#

oof no

sharp sun
#

Oh gods

topaz kraken
#

var is bad practise

sharp sun
#

how do I fix it then

topaz kraken
#

and converting to string is only ever used to put text on the screen

sharp sun
#

oh

#

ok

topaz kraken
#

otherwise pure lag

sharp sun
#

lmao

#

so i would have to change

#

the reception

#

to accept numbers

topaz kraken
#

it DOES use words

#

those words are variable names

sharp sun
#

what variable names?

topaz kraken
#

Speedster for a start

#

speedster has a speed and jump height

#

we know that one works

sharp sun
#

Yeah?

#

but heres the thing

#

the selectedrole variable

#

Actually wait

#

hold on

topaz kraken
#

nothings wrong with that, the code is way too simple for that to be the issue there

#

ChangeRole(MyRoleManager.GetComponent<RoleManager>().Speedster);

#

change the .Speedster for one of the other roles

sharp sun
#

ok where do i add that

topaz kraken
#

you dont

#

you already have that line of code

#

edit it

sharp sun
#

Oh wait let me

#

test speedster

#

Speedster still works the same

topaz kraken
#

so that code isnt broken, right

sharp sun
#

No

topaz kraken
#

in the role manager script, what other roles are there

sharp sun
#

speedster doesn’t have changes

#

which is bad

#

cause hes supposed to be faster

topaz kraken
#

what?

sharp sun
#

ok so all of the characters aren’t changed

topaz kraken
#

did you stop running the game

sharp sun
#

yes

#

ok but i think i know why it is

#

the rolemanager isn’t telling the code what role you are

topaz kraken
#

and do all of those roles in the manager script have different speeds?

#

no no no

#

no

#

thats not its job

sharp sun
#

the code has no idea what role we are

topaz kraken
#

it does

sharp sun
#

it kind of is

topaz kraken
#

in the player move script

#

you just ask for them

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

public class RoleManager : MonoBehaviour
{
    
    public SpriteRenderer sr;
    public List<Sprite> characters = new List<Sprite>();
    private int selectedcharacter = 0;
    public GameObject playercharacter;

    public Role Fighter;
    public float FighterSpeed;
    public float FighterJumpHeight;

    public Role Tanker;
    public float TankerSpeed;
    public float TankerJumpHeight;


    public Role Trickster;
    public float TricksterSpeed;
    public float TricksterJumpHeight;


    public Role Leaper;
    public float LeaperSpeed;
    public float LeaperJumpHeight;


    public Role Speedster;
    public float SpeedsterSpeed;
    public float SpeedsterJumpHeight;

    void Start()
    {
        Fighter.Speed = FighterSpeed;
        Fighter.JumpHeight = FighterJumpHeight;
        Tanker.Speed = TankerSpeed;
        Tanker.JumpHeight = TankerJumpHeight;
        Trickster.Speed = TricksterSpeed;
        Trickster.JumpHeight = TricksterJumpHeight;
        Leaper.Speed = LeaperSpeed;
        Leaper.JumpHeight = LeaperJumpHeight;
        Speedster.Speed = SpeedsterSpeed;
        Speedster.JumpHeight = SpeedsterJumpHeight;
    }

    public void NextOption()
    {
        selectedcharacter = selectedcharacter + 1;
        if (selectedcharacter == characters.Count)
        {
            selectedcharacter = 0;
        }
        sr.sprite = characters[selectedcharacter];
    }

    public void BackOption()
    {
        selectedcharacter = selectedcharacter - 1;
        if (selectedcharacter < 0)
        {
            selectedcharacter = characters.Count -1;
        }
        sr.sprite = characters[selectedcharacter];
    }

    public void PlayGame()
    {
        
    PrefabUtility.SaveAsPrefabAsset(playercharacter, "Assets/selectedcharacter.prefab");
    SceneManager.LoadScene("Game");
    }

}
#

look that over

topaz kraken
#

its perfect

sharp sun
#

oh

#

ok

topaz kraken
#

go into unity

sharp sun
#

Ok

topaz kraken
#

click the role manager object

sharp sun
#

im in unity

topaz kraken
#

look at the inspector

sharp sun
#

alr

#

Yes

topaz kraken
#

text boxes, put new numbers in there

sharp sun
#

they alr have numbers

topaz kraken
#

what numbers?

#

screenshot?

sharp sun
topaz kraken
#

and on the debug log, when you move, what value does it say when you are a fighter

sharp sun
#

Let me see

#

uh

#

it doesn’t say anything

#

Oh wiat

#

nvm

#

it says 10

#

even though he’s 8

#

i think I get whats happening

topaz kraken
#

did you change that line of code in the playerMove script to .Fighter instead of .Speedster?

sharp sun
#

no

topaz kraken
#

thats how you pick a role

sharp sun
#

But

#

I already have a role selection menu

#

Why do I do it through code?

topaz kraken
#

you dont have a role selection menu

sharp sun
#

yes I do

topaz kraken
#

youve got a menu that lets you decide what the values for all of the roles are

sharp sun
#

nonono

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;

public class RoleManager : MonoBehaviour
{
    
    public SpriteRenderer sr;
    public List<Sprite> characters = new List<Sprite>();
    private int selectedcharacter = 0;
    public GameObject playercharacter;

    public Role Fighter;
    public float FighterSpeed;
    public float FighterJumpHeight;

    public Role Tanker;
    public float TankerSpeed;
    public float TankerJumpHeight;


    public Role Trickster;
    public float TricksterSpeed;
    public float TricksterJumpHeight;


    public Role Leaper;
    public float LeaperSpeed;
    public float LeaperJumpHeight;


    public Role Speedster;
    public float SpeedsterSpeed;
    public float SpeedsterJumpHeight;

    void Start()
    {
        Fighter.Speed = FighterSpeed;
        Fighter.JumpHeight = FighterJumpHeight;
        Tanker.Speed = TankerSpeed;
        Tanker.JumpHeight = TankerJumpHeight;
        Trickster.Speed = TricksterSpeed;
        Trickster.JumpHeight = TricksterJumpHeight;
        Leaper.Speed = LeaperSpeed;
        Leaper.JumpHeight = LeaperJumpHeight;
        Speedster.Speed = SpeedsterSpeed;
        Speedster.JumpHeight = SpeedsterJumpHeight;
    }

    public void NextOption()
    {
        selectedcharacter = selectedcharacter + 1;
        if (selectedcharacter == characters.Count)
        {
            selectedcharacter = 0;
        }
        sr.sprite = characters[selectedcharacter];
    }

    public void BackOption()
    {
        selectedcharacter = selectedcharacter - 1;
        if (selectedcharacter < 0)
        {
            selectedcharacter = characters.Count -1;
        }
        sr.sprite = characters[selectedcharacter];
    }

    public void PlayGame()
    {
        
    PrefabUtility.SaveAsPrefabAsset(playercharacter, "Assets/selectedcharacter.prefab");
    SceneManager.LoadScene("Game");
    }

}
#

Look at the bottom

#

of this

#

im using this as a role selection

#

I shouldn’t

#

but I haven’t had time to fix it yet

topaz kraken
#

thats a character selector

sharp sun
#

No

#

wel

#

well

#

yes

#

Roles are Characters

#

in this context

#

I haven’t changed the old code yet

#

to be role

topaz kraken
#

then when you select a character, you need to tell it to get the role

sharp sun
#

ok

#

how do I do that

topaz kraken
#

youre doing too much with this script

#

its just for managing roles

sharp sun
#

Yeah I know

topaz kraken
#

youve got a sprite renderer in there

sharp sun
#

yeah so I should split it up

topaz kraken
#

yup

#

for now, you are able to assign a role to a character

#

which are very different things

#

yes its in code, but thats a good start

#

a real good start

sharp sun
#

let me do this rq

#

make a seperate script

topaz kraken
#

i sent you a link to the discord i run, im more than happy to open a project category for this stuff if need be

#

its only small

sharp sun
#

Yeah sure

#

maybe in a bit tho

#

Ok so

#

i seperate the two scripts (that are in one script)

#

into two seperate scripts

topaz kraken
#

you only make a script do one job

#

if its going to manage roles, it should do nothing else

#

the sprite renderer management needs to be its own script

#

the scene manager too

sharp sun
#

ok so uh

#

thats a lot to do

#

rn

topaz kraken
#

but thats a lot yeah

sharp sun
#

Idek how to do most of that

topaz kraken
#

which is why i said, for now, setting a role by just changing one word on a line of code is fine

#

youve got a load of refactoring and stuff to do before you can worry about something managing the characters and roles youve got automatically

#

otherwise spaghetti will happen fast, and thats like the developers version of a chronic illness

sharp sun
#

oh, I wanted it to be able to change it automatically

#

that was kind of the whole idea

topaz kraken
#

yes, but take steps

#

weve taken one of the first biggest steps so far

sharp sun
#

alrighty

topaz kraken
#

its so close to automatic, its changing a single word

sharp sun
#

true

#

so how do I keep working with you to code it?

topaz kraken
#

but like ive said, the other side of that equation, the management code and things, thats gotta be organized and tidied so that were not just layering things up and tangling things

sharp sun
#

yeah that makes sense

#

I don’t know if this is an issue or not

topaz kraken
#

its 00:46 where i am, lol

sharp sun
#

but I want to tell you before you help me any further

topaz kraken
#

ill need some sleep, thats why i mentioned my discord so we could organize a bit more of this down the line

sharp sun
#

i wanted this to be an online game, if that is okay

topaz kraken
#

never done online myself

sharp sun
#

but i felt

#

like i should mention

#

because just about everyone

topaz kraken
#

honestly, its not a fun thing to work on from what i hear

sharp sun
#

says online coding is hell

#

yeah, lots of darkwork

#

not fun

topaz kraken
#

definitely not, i will try online code at some point, but not soon lol

sharp sun
#

so will you still help me with the project?

topaz kraken
#

hmu in the discord whenever, theres a forum for unity things there

topaz kraken
sharp sun
#

yeah thats

#

what i meant

#

lol

#

alr well I can probably talk tomorrow

#

i should probably

#

eat

#

i’ve been working for 8 hours and I haven’t had anything literally all day

topaz kraken
#

basically me, i think its hot chocolate and cigarette time before i crash

sharp sun
#

Actually close to 9 hours now

sharp sun
#

sorry for keeping you up 😓

topaz kraken
#

so do i lol, enjoy food

sharp sun
#

Thanks

#

XD