Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Stormy102 · May 20, 2014 at 01:28 PM · javascriptainullreferenceexceptionnull

How can I stop getting Null Reference Exception?

I am making a FPS, but on my AI, I get a Null Reference Exception. I would like to make the script check if the tagged player exists, but I can't figure it out. Could someone please point me in the right direction? Here is the code:

 var Distance;
 var Target : Transform;
 var lookAtDistance = 250;
 //var attackRange = 15.0;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 //Prefabs for different hits.
 var FleshPrefab : Transform;
 var DirtPrefab : Transform;
 var MetalPrefab : Transform;
 var ConcretePrefab : Transform;
 var WoodPrefab : Transform;
 //Bullet holes
     public var woodBulletHole : GameObject[];
     //End of prefabs for differen hits.
     var gunshotSound : AudioClip;
     private var accuracy : float = 2.5;
     var TheDammage : int = 10;
     var attackRepeatTime = 3.5;
     var Health = 100;
     private var attackTime : float;
     var PlayerHealth : PlayerHealth;
     
     //Ragdoll and AI death
     var DeathRagdoll : GameObject;
     var AI : Transform;
     
     function Update ()
     {
         if (Target == null)
         {
             Target = GameObject.FindWithTag("Player").transform;
         }
         if (Target)
         {
             Distance = Vector3.Distance(Target.position, transform.position);
             
             if (Distance < lookAtDistance)
             {
                 renderer.material.color = Color.yellow;
                 lookAt();
             }
             
             if (Distance > lookAtDistance)
             {
                 renderer.material.color = Color.green;
             }
             
             /*if (Distance < attackRange)
             {
                 renderer.material.color = Color.red;
                 attack ();
             }*/
         }
     }
     
     function lookAt ()
     {    
         var rotation = Quaternion.LookRotation(Target.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
         attackRepeatTime = Random.Range(3.5,4.5);
         accuracy = Random.Range(0.01, 12.5);
         if (PlayerHealth.health >= 0)
         {
             Fire();
         }
     }
     
     function attack ()
     {
         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
     }
     
     function Fire()
     {
         if (Time.time > attackTime)
         {
             AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 10000);
             var hit : RaycastHit;
             var BulletDirection = transform.TransformDirection(new Vector3(Random.Range(-accuracy, accuracy) * 0.002, Random.Range(-accuracy, accuracy) *0.001,1));
             if (Physics.Raycast(transform.position, BulletDirection, hit, Mathf.Infinity))
             {
                 if (hit.transform.gameObject.tag=="Player")
                 {
                     hit.transform.SendMessage("Damage", TheDammage, SendMessageOptions.DontRequireReceiver);
                     var FleshClone = Instantiate(FleshPrefab, hit.point, transform.rotation);
                     Destroy(FleshClone.gameObject, 2);
                 }
                 if (hit.transform.gameObject.tag=="concrete")
                 {
                     var ConcreteClone = Instantiate(ConcretePrefab, hit.point, transform.rotation);
                     Destroy(ConcreteClone.gameObject, 2);
                 }
                 if (hit.transform.gameObject.tag=="dirt")
                 {
                     var DirtClone = Instantiate(DirtPrefab, hit.point, transform.rotation);
                     Destroy(DirtClone.gameObject, 2);
                 }
                 if (hit.transform.gameObject.tag=="metal")
                 {
                     var MetalClone = Instantiate(MetalPrefab, hit.point, transform.rotation);
                     Destroy(MetalClone.gameObject, 2);
                 }
                 if (hit.transform.gameObject.tag=="wood")
                 {
                     var WoodClone = Instantiate(WoodPrefab, hit.point, transform.rotation);
                     Destroy(WoodClone.gameObject, 2);
                     Instantiate(woodBulletHole[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
                 
                 }
             }
             attackTime = Time.time + attackRepeatTime;
         }
         
     }
     
     function ApplyDammage (TheDammage : int)
     {
         Health -= TheDammage;
         
         if(Health <= 0)
         {
             Dead();
         }
     }
     
     function Dead()
     {
         Instantiate (DeathRagdoll , AI.position, transform.rotation);
         Destroy (gameObject);
     }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Stormy102 · May 20, 2014 at 01:28 PM

I managed to fix it! Thanks to this post: http://answers.unity3d.com/questions/597652/missing-gameobject-null.html

I added this inside of it if(Target == null)

 if (Target == null)
     {
         if (GameObject.FindGameObjectWithTag("Player"))
         {
             Target = GameObject.FindWithTag("Player").transform;
         }
         else
         {
             return;
         }
     }


Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

null reference exception ... help! 2 Answers

Why is my array null? 0 Answers

NullReferenceException: Object reference not set to an instance of an object... 0 Answers

Checking name from hit from Raycast in array 2 Answers

Collsions problem 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