Search Unity

I get errors... Need help fixing :P

Discussion in 'Scripting' started by HanzoFactory, Nov 16, 2017.

  1. HanzoFactory

    HanzoFactory

    Joined:
    Nov 3, 2017
    Posts:
    1
    http://prntscr.com/hbb3ps - These are my error messages.

    The first one happens whenever I run the program and collide with an enemy (Which is supposed to do damage to the enemy and the player depending on the stats).

    The second one happens(/ed) when I save the script...

    My script is:

    In the "Player" script which the player has:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player : MonoBehaviour
    {
    public CapsuleCollider playerCollider;

    private Enemy enemyScript;

    private RaycastHit rayHit;
    private Ray ray;
    public float rayDistance = 4.0f;

    private int playerLevel = 0;

    private float manaDelay = 0.0f;
    private bool manaRegeneration = true;

    private bool manaShield = false;

    private float playerDamage = 10.0f;

    public float healthCur = 100.0f;
    public float manaCur = 100.0f;
    public float staminaCur = 100.0f;
    public float healthRegen = 10.0f;
    public float manaRegen = 10.0f;
    public float staminaRegen = 10.0f;
    private float healthMax = 100.0f;
    public float manaMax = 100.0f;
    private float staminaMax = 100.0f;

    public float moveSpeed = 5.0f;

    void Start()
    {
    playerCollider = GetComponent<CapsuleCollider>();
    playerCollider.height = 1.6f;
    playerCollider.center = new Vector3(0f, 0.8f, 0f);

    }

    // Update is called once per frame
    void Update()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    //Move the player.
    transform.Translate(movement * Time.deltaTime * moveSpeed);

    if (manaCur < manaMax)
    {
    manaCur += manaRegen * Time.deltaTime;
    }
    if (playerLevel > 0 && playerLevel <= 5 && manaRegeneration == true)
    {
    manaRegen = 10.0f;
    }

    if (healthCur < healthMax)
    {
    healthCur += healthRegen * Time.deltaTime;
    }
    if (playerLevel > 0 && playerLevel <= 5)
    {
    healthRegen = 10.0f;
    }

    if (staminaCur < staminaMax)
    {
    staminaCur += staminaRegen * Time.deltaTime;
    }
    if (playerLevel > 0 && playerLevel <= 5)
    {
    staminaRegen = 10.0f;
    }

    if (manaShield == true)
    {
    if (manaCur <= 0)
    {
    for (manaDelay = 0.0f; manaDelay <= 10.0f; manaDelay += 1.0f * Time.deltaTime)
    {
    manaRegeneration = false;
    }
    manaShield = false;
    }
    }

    if (Input.GetKeyDown(KeyCode.F))
    {
    manaShield = !manaShield;
    }

    }

    private void OnCollisionEnter(Collision collision)
    {
    if (collision.gameObject.tag == "Enemy")
    {
    if (manaShield == true)
    {
    if (manaCur > 0)
    {
    manaCur -= enemyScript.enemyDamage;
    }
    if (manaCur <= 0)
    {
    healthCur -= enemyScript.enemyDamage;
    }
    }
    else
    {
    healthCur -= enemyScript.enemyDamage;
    }

    enemyScript = collision.gameObject.GetComponent<Enemy>();
    enemyScript.enemyHealth = enemyScript.enemyHealth - playerDamage;

    }
    }
    }

    And in the one the enemy has:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Enemy : MonoBehaviour {

    public float enemyDamage = 40.0f;

    public float enemyHealth = 100.0f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    https://forum.unity.com/threads/using-code-tags-properly.143875/ Please fix your post with code tags.

    Also to note you can copy and paste errors from the console, or if you insist on using an image, just attach the image to the post instead of hosting it somewhere. :)

    The first "error" is just a warning and shouldn't effect your code any. Usually if it prompts me in visual studios I just tell it to fix it and no issues.

    The second is a null reference error. So something is null on line 125, but once you fix your post with code tags, we should be able to point to it better. Otherwise, you should be able to debug this yourself. Go to that line and see what is happening there and think, what could not have a value on that line. Use debug.logs to print out stuff and see what comes up null.
     
    JoeStrout likes this.