Hi, I have had a working script in JS that controls my character, but I am trying to re-write it in C#.
It does not error until I try to jump using the playerOneJumpTime routine, then I get the object reference error.
I have gone through the code over and over and I cant see why the jump coroutine is causing problems?
Please can someone help as I cant see the woods for the trees anymore.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class playerOneAttributes
{
public string playerOneName = "Player 1"; //Players name in game, can be changed
public int playerOneHealth = 100; //Players Total Current Health
public int playerOneRangePower = 100; //Players range damage (ie. fireballs, arrows)
public int playerOneMagicDamage = 100; //Players spell damage from potions
public int playerOneMeleeDamage = 100; //Players melee damage
public int playerOneDefense = 10; //Players Defense
public float playerOneMoveSpeed = 100; //Players movement speed
public int playerOneXP = 0; //Players XP
}
[System.Serializable]
public class playerOnelevelUpMultipliers
{
public float playerOneHealthMultiplier = 1.5f; //Players health multplier upon level up
public float playerOneRangeMultiplier = 1.5f; //Players range power multplier upon level up
public float playerOneMagicMultiplier = 1.5f; //Players magic power multplier upon level up
public float playerOneMeleeMultiplier = 1.5f; //Players melee power multplier upon level up
public float playerOneDefenseMultiplier = 1.5f; //Players defense multplier upon level up
}
[System.Serializable]
public class playerOneControls
{
public string playerOneControlUp = "w"; //Player Controls (re-mappable)
public string playerOneControlDown = "s"; //Player Controls (re-mappable)
public string playerOneControlLeft = "a"; //Player Controls (re-mappable)
public string playerOneControlRight = "d"; //Player Controls (re-mappable)
public string playerOneControlJump = ","; //Player Controls (re-mappable)
public string playerOneControlAttack = "."; //Player Controls (re-mappable)
public string playerOneControlMagic = "/"; //Player Controls (re-mappable)
}
public class playerOnePhysics
{
public float playerOneJumpRate = 1.1f; //Players time between jumps
public float playerOneJumpPower = 5.0f; //Players Jump Power
}
[System.Serializable]
public class playerOneStatus
{
public bool playerOneAlive = true; //Is player one alive?
public bool playerOneIsIt = false; //Is player IT?
public bool playerOneStunned = false; //Is player one stunned?
public bool playerOnePoisoned = false; //Is player one poisoned?
}
[System.Serializable]
public class playerOneInventory
{
public int playerOnePotionAmount = 0; //Magic potion amount
public int playerOneKeyAmount = 0; //Number of keys held
public bool playerOnePowerUpOne = false; //Extra Shot Power
public bool playerOnePowerUpTwo = false; //Extra Melee Power
public bool playerOnePowerUpThree = false; //Extra Magic Power
public bool playerOnePowerUpFour = false; //Extra Speed
public bool playerOnePowerUpFive = false; //Extra Armour
public bool playerOnePowerUpSix = false; //Extra Fight Power
public bool playerOnePowerUpSeven = false; //Extra XP
}
public class ScriptPlayerOneManager : MonoBehaviour
{
public playerOneAttributes playerAttributes; //These show the variables in the inspector
public playerOnelevelUpMultipliers levelUpMultipliers;
public playerOneControls playerControls;
public playerOnePhysics playerPhysics;
public playerOneStatus playerStatus;
public playerOneInventory playerInventory;
// Use this for initialization
void Start()
{
rigidbody.freezeRotation = true; // Prevent the player from falling over due to physics
StartCoroutine(playerOneJumpTime());
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(playerControls.playerOneControlUp))
{
transform.position += new Vector3(0, 0, 0.035f * playerAttributes.playerOneMoveSpeed) * Time.deltaTime;
}
if (Input.GetKey(playerControls.playerOneControlDown))
{
transform.position -= new Vector3(0, 0, 0.035f * playerAttributes.playerOneMoveSpeed) * Time.deltaTime;
}
if (Input.GetKey(playerControls.playerOneControlRight))
{
transform.position += new Vector3(0.035f * playerAttributes.playerOneMoveSpeed, 0, 0) * Time.deltaTime;
}
if (Input.GetKey(playerControls.playerOneControlLeft))
{
transform.position -= new Vector3(0.035f * playerAttributes.playerOneMoveSpeed, 0, 0) * Time.deltaTime;
}
}
IEnumerator playerOneJumpTime() // This function controls the players jump - makes the player wait to be able to jump again
{
while (true)
{
if (Input.GetKeyDown(playerControls.playerOneControlJump))
{
rigidbody.velocity = new Vector3(0, playerPhysics.playerOneJumpPower, 0);
yield return new WaitForSeconds(playerPhysics.playerOneJumpRate);
}
else
{
yield return null;
}
}
}
}