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 MoonwalkingBaz · May 18, 2015 at 10:45 AM · c#referencenullexception

Null Reference Exception error

alt text

Hi guys

new to C# (Kinda) Getting a null reference error on Line 22 (ill post the full code below, but you can see the error and line 22 in the image, where the error is pointing to. any ideas on how to fix? Its baffled me.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SF_PatrolEnemyMove : SF_Enemy_Move 
 {
     //list to hold waypoinmts for movement
     public List<Vector3> mLS_V3_Waypoints = new List<Vector3>();
 
     //Holds indec in mLS_V3_Waypoints that is current target
     private int mIN_TargetIndex = 0;
 
     //Holds all colliders that the enemy vision collider hits
     public List<Collider> mHitTargets = new List<Collider>();
 
 
     // Use this for initialization
     void Start () 
     {
         base.Start ();
         mST_State = "patrol";
         mTM_StateText.text = mST_State;
         HP = 20;
         Defence = 8;
     }
     //initialise patrol path for enemy
     public void StartPatrol(List<Vector3> vLS_V3_Waypoints, float vFL_Speed)
     {
         mFL_Speed = vFL_Speed;
         foreach(Vector3 tV3_Waypoint in vLS_V3_Waypoints)
         {
             mLS_V3_Waypoints.Add (tV3_Waypoint);
         }
         mST_State = "patrol";
         mTM_StateText = GetComponentInChildren<TextMesh> ();
         mTM_StateText.text = mST_State;
         mV3_TargetPos = mLS_V3_Waypoints [0];
     }
 
     //method to switch states of enemy by triggers
     protected override void Triggers()
     {
         if(mMB_PCScript.ST_State == "carry" && mST_State != "flee")
         {//change to flee state if PC is in carry
             mST_State = "flee";
             mTM_StateText.text = mST_State;
         }
         if(mMB_PCScript.ST_State == "walk" && mST_State == "flee")
         {//change to chase state if PC is in walk state and NPC was just fleeing
             mST_State = "chase";
             mTM_StateText.text  = mST_State;
         }
         if(mST_State == "chase" && mFL_TargetDistance > 10F)
         { //change to roam state id PC is outside of a certain range
             mST_State = "patrol";
             mTM_StateText.text = mST_State;
         }
     }
 
     //---------------TRIGGERS------------
 
     void OnTriggerEnter(Collider vCL_Spotted)
     {
         //if collider is not already in mHitTargets List and not
         //tagged ignore like our waypoint markers
         if(mHitTargets.Find(x => x == vCL_Spotted) == null && vCL_Spotted.transform.tag != "ignore")
         {
             mHitTargets.Add (vCL_Spotted);
         }
         //createt a collider to hold closest object we collide with
         Collider tCL_Closest = null;
 
         //variable to store closest distance
         float mFL_ClosestDis = 99;
 
         //loop through all colliders in list
 //        foreach(Collider col in mHitTargets)
 //        {
 //            if(col.transform!=null)
 //            {
 //                //if distance between object collided with is less than previous distance of currrenly closest object then update
 //                if(Vector3.Distance (col.transform.position, transform.position) < mFL_ClosestDis)
 //                {
 //                    mFL_ClosestDis = Vector3.Distance(col.transform.position, transform.position);
 //                    tCL_Closest = col;
 //                }
 //            }
 //        }
 
         //if closest object is not null this only causes problems if object collided with is tagged ignore
         if(tCL_Closest != null)
         {
             if(tCL_Closest.gameObject.tag == "Player")
             {//if player is closest object then chase
                 mST_State = "chase";
                 mTM_StateText.text = "!";
                 mTM_StateText.color = Color.red;
             }
         }
         tCL_Closest = null;
         mFL_ClosestDis = 99;
     }
 
     //Method called on the frame an objct stops colliding with the enemy
     void OnTriggerExit(Collider vCL_Spotted)
     {
         mHitTargets.Remove (vCL_Spotted);
     }
 
     //Our method to called state movement methods
     protected override void Move()
     {
         base.Move ();
 
         if (!mLM_LevelManager.isPaused())
         {
             //resolve game states
             switch (mST_State)
             {//Call appropriate methods to deal with movement in current state
             case "patrol": Patrol(); break;
             }
         }
     }
 
     //method defining movement in patrol state
     void Patrol()
     {
         if (Vector3.Distance (mV3_CurrentPos, mGO_Character.transform.position) > 0.1F)
         {
             mBL_Moving = true;
             //find the vector pointing from our position to the target
             Vector3 tV3_Direction = (mV3_CurrentPos - mGO_Character.transform.position).normalized;
 
             //create the rotation we need to be in to look at the target
             Quaternion tQT_Rotation = Quaternion.LookRotation(tV3_Direction);
             mBL_Moving = true;
             //Rotate gradually to target instead of instantly
             mGO_Character.transform.rotation = Quaternion.Lerp (mGO_Character.transform.rotation,
                                                                 tQT_Rotation, 15 * Time.deltaTime);
             mGO_Character.transform.position = Vector3.MoveTowards (mGO_Character.transform.position,
                                                                     mV3_CurrentPos, mFL_Speed * Time.deltaTime);
         }
         else if(Vector3.Distance (mV3_TargetPos, mGO_Character.transform.position) > 0.1F)
         {
             ChooseNextPosition();
         }
         else
         {
             transform.position = mV3_TargetPos;
         }
     }
 
     //Method to set movement target for all states
     protected override void SetTarget()
     {
         //patrol state set target
         if (mST_State == "patrol")
         {
             if(mIN_TargetIndex == 0)
             {
                 SetPatrolTarget(mLS_V3_Waypoints[0]);
             }
             else
             {
                 SetPatrolTarget(mLS_V3_Waypoints[mIN_TargetIndex]);
             }
             ComputeTargetDistance();
 
             //if the NPC has reached the target waypoint choose another
             if (mFL_TargetDistance < 0.1F)
             {
                 if(mIN_TargetIndex == (mLS_V3_Waypoints.Count - 1))
                 {//if reached end of list then choose the first waypoint again
                     SetPatrolTarget(mLS_V3_Waypoints[0]);
                     mIN_TargetIndex = 0; //update index
                 }
                 else
                 {
                     SetPatrolTarget(mLS_V3_Waypoints[mIN_TargetIndex + 1]);
                     mIN_TargetIndex++; //update index
                 }
             }
         }
         else
         {//all other states use base class settarget()
             base.SetTarget();
         }
 
     }//--------------
     void SetPatrolTarget(Vector3 vV3_Pos)
     {
         mV3_TargetPos = vV3_Pos;
     }
     
 }
 


screen-shot-2015-05-18-at-105439.png (87.4 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Halfbiscuit · May 18, 2015 at 10:58 AM

If you look further down at line 34,35 and 36 you can see this:

mST_State = "patrol";

mTM_StateText = GetComponentInChildren ();

mTM_StateText.text = mST_State;

'mTM_StateText' is being initialised at line 35 but in your start function you forgot to do this.

mTM_StateText.text cannot be set because mTM_StateText is currently null.

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
avatar image
0

Answer by Cherno · May 18, 2015 at 10:58 AM

I don't see neither mST_State nor mTM_StateText being declared anywhere.

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
avatar image
0

Answer by MoonwalkingBaz · May 18, 2015 at 11:09 AM

mST_State = "patrol"; mTM_StateText.text = mST_State;

Both variables are being declared in the start function, line21 + 22

Comment
Add comment · Show 9 · 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 Halfbiscuit · May 18, 2015 at 11:20 AM 0
Share

Well no they are not being declared on those lines, they are being set. I am assu$$anonymous$$g the variables were declared in the parent class: SF_Enemy_$$anonymous$$ove.

avatar image MoonwalkingBaz · May 18, 2015 at 11:30 AM 0
Share

yes they are

sorry, im a newbit at unity,, how would i fix it?

avatar image Halfbiscuit · May 18, 2015 at 11:34 AM 0
Share

If you look at the answer I gave above you need this line(or any line that sets mT$$anonymous$$_StateText):

mT$$anonymous$$_StateText = GetComponentInChildren ();

before you set the text variable in line 22.

avatar image MoonwalkingBaz · May 18, 2015 at 11:37 AM 0
Share

and should i put that where all the other variables are being initialised or within a function?

avatar image Halfbiscuit · May 18, 2015 at 11:42 AM 0
Share

Basically change your start function to this:

void Start ()

{

  base.Start ();
  mST_State = "patrol";
  mT$$anonymous$$_StateText = GetComponentInChildren();
  mT$$anonymous$$_StateText.text = mST_State;
  HP = 20;
  Defence = 8; 

}

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Despawner script returns null reference exception. 0 Answers

Instantiate() as GameObject = null reference 1 Answer

Confused as to why I'm getting a NullReference Exception 1 Answer

C# Null Object Reference - Driving me mad! 1 Answer

Null Reference Exception After refering to an Array 1 Answer


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