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 davidflynn2 · Aug 22, 2013 at 06:25 PM · c#aihealth

Health not being subtracked.

I have the following code and it is basically my AI system fro my enemy. It controls targeting and attacking. I just added the attack() but I am get a the following error. In the health script it is trying to axcess is called ShipHealth.cs and this is where I am trying to send the information :

public void AddjustCurrentHealth(int adj)

NullReferenceException: Object reference not set to an instance of an object EnemyAi.attack () EnemyAi.cs:109)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic; 
 
 public class EnemyAi : MonoBehaviour 
 {
     //public Transform target;//Targets.
     public int rotationSpeed;//Speed enemy can rotate.
     public List<Transform> targets = new List<Transform>();//List of targets.
     public GameObject Gun;//Where the laser could come from.
     public int Damage = -10;//This should be negative or you are going to heal it.
     //public GameObject target;
     private LineRenderer LR;
     
     private float arcLength = 2.0f;
     private float arcVariation = 0f;
     private float inaccuracy = 0f;
     
     private Vector3 fwd;
 
     
     
     private Transform myTransform;//Save for the transform.
     void Start()
     {
         LR  = Gun.GetComponent<LineRenderer>();
 
     }
     void Awake()
     {
         
         myTransform = transform;//This saves our transform so we dont have to look it up all the time.    
         
         
     }
     
     void Update ()
     {
         
         if(targets.Count < 1) 
         {
             Gun.GetComponent<LineRenderer>().enabled = false;
             return;//If there is no targets sets targets to 0.
         }
         
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targets[0].position - myTransform.position), rotationSpeed * Time.deltaTime);//Makes it look at us using the speed we set over time.
         
         Laser();
         attack();
         
  }
     void Laser()
     {
         Vector3 lastPoint = transform.position;
         
         
         int i = 1;
 
         LR.SetPosition(0, transform.position);//make the origin of the LR the same as the transform   
          while (Vector3.Distance(targets[0].transform.position, lastPoint) >.5) 
              {//was the last arc not touching the target?            
                  LR.SetVertexCount(i + 1);//then we need a new vertex in our line renderer          
                  fwd = targets[0].transform.position - lastPoint;//gives the direction to our target from the end of the last arc20.         
                  fwd.Normalize();//makes the direction to scale         
                   fwd = Randomize(fwd, inaccuracy);//we don't want a straight line to the target though      
                   fwd *= Random.Range(arcLength * arcVariation, arcLength);//nature is never too uniform    
                  fwd += lastPoint;//point + distance * direction = new point. this is where our new arc ends     
                   LR.SetPosition(i, fwd);//this tells the line renderer where to draw to25.         
                  i++;         
                  lastPoint = fwd;//so we know where we are starting from for the next arc  
               }
         
     }
      Vector3 Randomize(Vector3 v3 ,   float inaccuracy2)
     {
         
         v3 += new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * inaccuracy2; 
           v3.Normalize(); 
            return v3; 
         
     }
         
     
     
      void OnTriggerEnter(Collider other) //When somthing enters the trigger.
     {
         if(other.tag == "Player")//Check if it was tagged player.
         {
             targets.Add(other.transform);//If so add it to the targets list.
             Gun.GetComponent<LineRenderer>().enabled = true;
         }
         
         
     }
     void OnTriggerExit(Collider other) //When somthing exits the trigger.
     {
         if(other.tag == "Player")//Check if it was tagged player.
         {
             targets.Remove(other.transform);//If so remove it from the targets list
             
         }
         
         
     }
     private void attack()
     {
         print("I have made it this far");
         ShipHealth sh = (ShipHealth)targets[0].GetComponent("ShipHeatlh");
         sh.AddjustCurrentHealth(Damage);
         
     }
     
     
 }
 
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
2
Best Answer

Answer by clunk47 · Aug 22, 2013 at 06:28 PM

AddjustCurrentHealth has an extra 'd'. I'd imagine it's spelled correctly in the other script? Besides that, instead of:

 ShipHealth sh = (ShipHealth)targets[0].GetComponent("ShipHeatlh");
 sh.AddjustCurrentHealth(Damage);

Looks like you're just going for the first index of an array, so just use GetComponent like so:

 targets[0].GetComponent<ShipHealth>().AdjustCurrentHealth(Damage);

Assuming AdjustCurrentHealth is spelled the same way in your ShipHealth Script.

Comment
Add comment · Show 2 · 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 meat5000 ♦ · Aug 22, 2013 at 07:16 PM 0
Share

Is there a script called LineRenderer attached to the Gun object?

avatar image davidflynn2 · Aug 22, 2013 at 07:42 PM 0
Share

Ok sorry about last question computer glitch had to restart.

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

17 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

Related Questions

Distribute terrain in zones 3 Answers

Need help calling a variable from another C# script 1 Answer

Multiple Cars not working 1 Answer

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

Enemy AI C# 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