#Monodeath's Code Error
1 messages · Page 2 of 1
no
Ok
Yeah but what’s supposed to be there
anything that is a role
yeah no, the error says An object reference is required for the nonstatic field, method, or property
Oh okay
something isnt static, or we need an object reference
so, hows the void looking, static?
undo that then
yeah
not the object reference for its script's value
yes
yes
put the whole movement class in here, as text
the screenshots are mega confusing me atm
im working off memory of bits
Gimme the template rq
then i can edit it to fix the thing
I see it
no spaces before csharp
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;
}
}
}
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
Do i just copy paste that?
yes
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
It works!
ay, and the code is clean too
awesome!
cant hurt to maybe refactor a little bit though, but for now its ok
so movement should work now right?
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
the roles arent set properly?
The rolemanager doesnt use words
it uses numbers
so if i just make it convert it to string var
it should work
oof no
Oh gods
var is bad practise
how do I fix it then
and converting to string is only ever used to put text on the screen
otherwise pure lag
what variable names?
Speedster for a start
speedster has a speed and jump height
we know that one works
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
ok where do i add that
so that code isnt broken, right
No
in the role manager script, what other roles are there
what?
ok so all of the characters aren’t changed
did you stop running the game
yes
ok but i think i know why it is
the rolemanager isn’t telling the code what role you are
and do all of those roles in the manager script have different speeds?
no no no
no
thats not its job
the code has no idea what role we are
it does
it kind of is
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
its perfect
go into unity
Ok
click the role manager object
im in unity
look at the inspector
text boxes, put new numbers in there
they alr have numbers
and on the debug log, when you move, what value does it say when you are a fighter
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
did you change that line of code in the playerMove script to .Fighter instead of .Speedster?
no
thats how you pick a role
you dont have a role selection menu
yes I do
youve got a menu that lets you decide what the values for all of the roles are
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
thats a character selector
No
wel
well
yes
Roles are Characters
in this context
I haven’t changed the old code yet
to be role
then when you select a character, you need to tell it to get the role
Yeah I know
youve got a sprite renderer in there
yeah so I should split it up
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
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
Yeah sure
maybe in a bit tho
Ok so
i seperate the two scripts (that are in one script)
into two seperate scripts
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
but thats a lot yeah
Idek how to do most of that
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
alrighty
its so close to automatic, its changing a single word
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
its 00:46 where i am, lol
but I want to tell you before you help me any further
ill need some sleep, thats why i mentioned my discord so we could organize a bit more of this down the line
i wanted this to be an online game, if that is okay
yeah, ofc get some rest
never done online myself
honestly, its not a fun thing to work on from what i hear
definitely not, i will try online code at some point, but not soon lol
so will you still help me with the project?
hmu in the discord whenever, theres a forum for unity things there
i can help with learning c# and unity
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
basically me, i think its hot chocolate and cigarette time before i crash
Actually close to 9 hours now
welp hope you get some good rest
sorry for keeping you up 😓
so do i lol, enjoy food