Okay, so what’s wrong here? I am making a simple dodge game, and what I want my script to do is tell the enemy’s transform to go back between a set random position along the x and z axis, relative to the players position, after it passes by the player… The PlayerMove script has been attached to the player. My problem is that is looks as if I am not getting a link between the player and enemy scripts. These scripts work if I set all of the public float variables to Static, but then they won’t show up in the inspector. So, I tried to make a reference to my other scripts and this is what I have come up with:
Enemy Script:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
private float x,y,z;
public float zAxis;
public float moveSpeed;
public float minSpeed = -5;
public float maxSpeed = -7;
public PlayerMove playerMove;
// Use this for initialization
void Start ()
{
RandomPosition ();
}
// Update is called once per frame
void Update ()
{
//Random speed between minSpeed and maxSpeed.
moveSpeed = Random.Range (maxSpeed,minSpeed);
zAxis = transform.position.z;
if (zAxis <= -10)
{
RandomPosition();
minSpeed = minSpeed - .07f;
maxSpeed = maxSpeed -.07f;
}
transform.Translate(Vector3.forward * moveSpeed* Time.deltaTime);
}
//Assign a random position along the X axis, but within the players perspective.
void RandomPosition()
{
float xAxis = GameObject.Find("Player").GetComponent<PlayerMove>().xAxis;
x = Random.Range(xAxis -15f, xAxis + 15f);
y = 2.5f;
z = Random.Range (10f, 15f);
transform.position = new Vector3(x,y,z);
}
}
PlayerMove Script:
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float playerSpeed = 10;
public float forwardSpeed;
public float xAxis;
public float distance;
public Enemy enemy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
forwardSpeed = GameObject.Find("Enemy").GetComponent<Enemy>().moveSpeed;
xAxis = transform.position.x;
MovePlayer ();
}
void MovePlayer(){
// Ammount to Move the player
float amtToMove = Input.GetAxis("Horizontal") * playerSpeed;
//Method for moving the player
transform.Translate(Vector3.right * amtToMove * Time.deltaTime);
}
//Calculate the distance, and display it on the screen.
void OnGUI(){
distance = (forwardSpeed * Time.timeSinceLevelLoad / 3) * -1;
GUI.Label (new Rect(10,10,300,20), "Distance: " + distance.ToString() + " Kilometers");
}
}
In JavaScript, I would be able to just make a variable public playerMove : PlayerMove, and call it by using playerMove.xAxis, but with C# it gives me a NullReferenceException.
The error messages I keep getting read as follows:
NullReferenceException: Object reference not set to an instance of an object
Enemy.RandomPosition() (at Assets/Scripts/Enemy Scripts/Enemy.cs:38)
NullReferenceException: Object reference not set to an instance of an object
PlayerMove.Update() (at Assets/Scripts/Player Scripts/PlayerMove.cs:19)
I know it’s not the best code in the world, but it’s pretty much all I know how to do… I think I’ve done good for what I know, at least… Please, please help. I don’t know what’s wrong here…
Thank you in advance!