- Home /
Player not moving due to static instance : is dynamic instancing a thing ?
Hello ! I recently had to do add a "static instance" to a script on my player that is supposed to make my player move. I relaunched my unity today, runned the project and the joysticks aren't working anymore. The moving system is made of a Joystick with script and my player with a "MovePlayer" script. The velocity values are well received from the joysticks to the MovePlayer script, but my player doesn't move when I try to. It also just refuses to move in game mode, even by modifying in the inspector the transform.
My two questions are : - Can you confirm that he problem is the "static instance" line ? - How can I fix my problem ?
Sorry for the dumb questions, I'm a beginner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovePlayer : MonoBehaviour
{
public static MovePlayer instance;
public MovementJoystick movementJoystick;
public float moveSpeed;
private Rigidbody2D rb;
public Vector3 m_velocity = Vector3.zero;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
if(movementJoystick.joystickVec.y != 0)
{
Vector3 targetVelocity = new Vector2(movementJoystick.joystickVec.x * moveSpeed, movementJoystick.joystickVec.y * moveSpeed);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref m_velocity, .05f);
}
else
{
rb.velocity = Vector2.zero;
}
}
}
Hi, I dont think its your instance variable, simply because you are not setting / reading it in this code excerpt. I can't tell you how to fix it though (apart from have a look at debugging ;) ), I would need a bigger code base for that. If you upload your reduced gamecode to github/dropbox i can have a look at it.
Answer by CycloneWhale · Aug 24, 2021 at 12:53 AM
There's no such thing as a dumb question, don't worry we've all spent hours asking questions on Unity Answers at some point.
As for your static instance, it doesn't seem that you ever assign it a value. Usually with singletons like that, in the Awake() function, you have something like this:
private void Awake()
{
if (instance == null)
instance = this;
}
The variable instance still has to be assigned a value at some point. You could have multiple "MovePlayer" scripts, so you have to tell it which one.
Anyways I hope that helps!