Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Sep 01, 2016 at 12:07 PM by mnmwert for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mnmwert · Aug 29, 2016 at 08:32 PM · scripting problemnullreferenceexception

NullReferanceExeption Unity 5.2 C# script

I have a script for an object. the objects goal is to be destroyed by the player and give the player score. the problem is that I get a nullreferenceexeption when trying to define my player.

 using UnityEngine;
 using System.Collections;
 
 
 public class blockBehavior : MonoBehaviour
 {
     public int health;
 
     public float time;
 
     private GameObject player;
 
     public int value;
     
     void Start ()
     {
         player = GameObject.FindWithTag("Player");
     }
     
     
     void Update ()
     {
         player = GameObject.FindWithTag("Player");
         if(player == null)
         {
             Debug.Log("cannot find the object: Player");
         }
 
         if (health <= 0)
         {
             player.GetComponent<playerMovement>().score += value;
             Destroy(gameObject);
         }
        
     }
     void FixedUpdate()
     {
         time -= 1;
         if(time < 0)
         {
             Destroy(gameObject);
         }
     }
     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "bullet")
         {
             health -= 1;
         }
         if(collision.gameObject.tag == "Player")
         {
             player.GetComponent<playerMovement>().score += value;
             Destroy(gameObject);
         }
     }
 }

the reference exeption seems to haft to do with

player = GameObject.FindWithTag("Player");

but I dont know why

Thanks in advance!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mnmwert · Aug 31, 2016 at 10:37 PM 0
Share

I dont want the player to be destroyed after a certain amount of time, I want the object (I cannot call it an enemy because I have a different object as an enemy) to be destroyed after a long time (3000 seconds) so that it will not make the game lag and so I have room for other objects in the scene. my player if it collides with the object will destroy the object and give it score, while if you shoot it with the player's bullet (not to be confused with the enemy's bullet) it will decrease its health by 1. After a while, It will destroy the object and give the player more score. it is only their for you to destroy it and increase your score. if that makes sense

avatar image TBruce mnmwert · Aug 31, 2016 at 11:05 PM 1
Share

With your original script and description and the fact that you have attached the script to player and your recent comments do not make sense.

You need to outline exactly what you are doing. For example

  1. You have a player

  2. You have an object

  3. You have a movement script.

  4. What exactly you want done to the object (I know you just explained it a little more)

  5. What exactly you want done to the player

As said previously, you will need at a $$anonymous$$imum a script that has to attach to the object (I don't care what you call it, I said enemy as an example but you should be as descriptive as possible).

You can first try taking your original script and ins$$anonymous$$d attach it to the object and not the player as you mentioned you were doing. That is probably a step closer to what you want.

And remove this

 player = GameObject.FindWithTag("Player");

from the Update function, it only needs to be done once in start.

You need to make sure that the player GameObject is tagged properly though.

1 Reply

  • Sort: 
avatar image
0

Answer by mnmwert · Sep 01, 2016 at 12:01 PM

I have a player in a scene who you can move around a shoot with, As I gain score by shooting things I will level up:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class playerMovement : MonoBehaviour
 {
     public float speed;
     public float rotationSpeed;
     public Transform Target;
     public Transform target1;
 
     public GameObject Bullet;
     
     public float fireSpeed, fireRate,f1,f2;
     public Transform Spawn;
     public Transform Spawn1;
 
     public int health;
     public float healthRegene, healthres;
     public int maxHealth;
 
     public int score;
     public int level;
     public Text level1;
 
     public bool duel = false;
    
     void Start ()
     {
         
     }
 
 
     void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.W))
         {
             transform.position = Vector3.MoveTowards(transform.position,Target.position,speed); 
         }
         if (Input.GetKey(KeyCode.A))
         {
             transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.D))
         {
            transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.S))
         {
             transform.position = Vector3.MoveTowards(transform.position, target1.position, speed); 
         }
         if (Input.GetKey(KeyCode.Mouse0) && Time.time > fireRate)
         {
            fireRate = Time.time + fireSpeed;
            Instantiate(Bullet,Spawn.position,Spawn.rotation);
         }
         if (Input.GetKey(KeyCode.Mouse1) && duel == true && Time.time > f1)
         {
           
             f1 = Time.time + f2;
             Instantiate(Bullet, Spawn1.position, Spawn1.rotation);
         }
 
         if(health <= 0)
         {
             Destroy(gameObject);
            
         }
         if (Time.time > healthRegene && health <= maxHealth)
         {
             healthRegene = Time.time + healthres;
             health += 1;
         }
         level = level1.GetComponent<LevelUp>().level;
         if(level >= 5)
         {
             fireRate = .2f;
         }
         if(level >= 10)
         {
             maxHealth = 250;
         }
         if(level >= 15)
         {
             speed = 2;
         }
         if(level >= 20)
         {
             healthRegene = .5f; 
         }
         if(level >= 25)
         {
             duel = true;
         }
         if(level < 25)
         {
             duel = false;
         }
         if(level >= 30)
         {
             Bullet.GetComponent<bulletSpeed>().life = 160;
         }
         f1 = fireRate;
         f2 = fireSpeed;
     }
     void OnCollisionEnter(Collision collision)
     {
        if(collision.gameObject.tag == "object")
         {
             health -= 1;
         }
        if(collision.gameObject.tag == "enemybullet")
         {
             health -= 1;
         }
        if(collision.gameObject.tag == "enemy")
         {
             health -= 1;
         }
     }
 
 }
 

I have my block script to control the block, and a score and level scripts to control those. I want the object to be destroyed by: Time, The Player, The Players Movement. I want the Player to move around in the game. When The player does something that ensues damage, His health decreases by 1 or more (depending on the significance). If his health is 0 you go to the game over screen.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mnmwert · Sep 01, 2016 at 12:07 PM 0
Share

I found the problem! it had nothing to do with my scripting! my enemy was tagged player so when it was destroyed it couldn't find the player! Thanks for helping!

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException in FiniteStateMachine with ThirdPersonCharacter 0 Answers

Why do i get NullReferenceException here? 1 Answer

Boolean won't set false? 0 Answers

Null Reference Exception on an unused script 1 Answer

Script works in editor but when the game loads an asset bundle with it, all the serialized data is lost. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges