I’m essentially trying to keep player movement in a separate script ( as well as separate scripts for attack, health etc later).
Here is part of my Player.cs script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//configuration parameters
[SerializeField] float moveSpeed = 100f;
[SerializeField] float padding = 0.7f;
[SerializeField] GameObject laserPrefab;
[SerializeField] float projectileSpeed = 10f;
[SerializeField] float projectileFiringPeriod = 0.1f;
Coroutine firingCoroutine;
float xMin;
float xMax;
float yMin;
float yMax;
public PlayerMove playerMove;
// Start is called before the first frame update
void Start()
{
SetUpMoveBoundaries();
}
// Update is called once per frame
void Update()
{
playerMove.movement();
Fire();
}
And here is my PlayerMove.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove: MonoBehaviour
{
float moveSpeed = 100f;
float xMin;
float xMax;
float yMin;
float yMax;
public void movement()
{
float deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
float newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
float newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
transform.position = new Vector2(newXPos, newYPos);
}
}
I’m getting
NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Scripts/Player.cs:33) as an error. I get the same error if I add
playerMove = GetComponent<PlayerMover>();
Now from what I’ve learned this is happening because PlayerMove.cs is a component which needs to be added to a game object. What game object would I add this to since ideally only Player.cs would be attached to the player game object? Or is there an alternative way for me to call the movement method from PlayerMove.cs in Player.cs? Appreciate the help
The error references line 33 of the player script. Is there more to the script than what you’ve put here? If so it might help to see the whole script to figure out what’s wrong.
Yeah that’s because PlayerMove isn’t assigned to the playerMove variable in Player. You won’t be able to assign the script itself to that variable either as far as I know, it would need to be attached to the player.
One other way to do this could be to make the Player script inherit from PlayerMove.
I don’t know if this is the best way to do it at all. Just the first thing that came to mind. Like Olmi said it would probably make more sense to just assign all scripts to their relevant objects.
// PLAYER SCRIPT INHERITING FROM PLAYERMOVE
public class Player : PlayerMove
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Movement();
}
}
//PLAYER MOVE SCRIPT
public class PlayerMove : MonoBehaviour
{
float moveSpeed = 100f;
float xMin;
float xMax = 10;
float yMin;
float yMax = 10;
public void Movement()
{
float deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
float newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
float newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
transform.position = new Vector2(newXPos, newYPos);
}
}
The idea is that I have 3 different characters the player can choose from, so each character would inherit this player class (as well as an interface for unique features of each character). I could technically add everything inside the player class but to avoid all the messiness I’d rather split it into different scripts movement, attack, health etc. and then call them within the player script. Is there any point in making a player script in my case then? Should i just make multiple scripts for movement, attack, health etc and assign all of them to each of my 3 player objects