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 /
This question was closed Feb 24, 2013 at 06:39 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by anonnewguy · Feb 24, 2013 at 04:45 AM · c#aiinstancingnull exception

Trouble accessing a class variable from another class in C#.

Hi, I'm sure this is a simple mistake, but I've looked over several answers here and cannot figure out what I'm doing wrong in terms of properly instancing my GameObject. This is a simple AI script in which I want the NPC to either approach the MainCharacter or Run from the MainCharacter depending on the value of an int in the MainCharacter class, so I'm attempting to access the value in the AI script. Here's the relevant parts of my MainCharacterScript.cs using UnityEngine; using System.Collections;

 public class MainCharacterScript : MonoBehaviour {
     
     public int playercolor;
 
     // Use this for initialization
     void Start () {
     playercolor = 0;
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public int GetPlayerColor(){
         return playercolor;
     }
 }






and here's the relevant (and offending) parts of my AIScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class AIScript : MonoBehaviour {
     
     int playercolor;
     int mycolor = 1;
         float FollowSpeed = 2;
     float FleeSpeed = 8;
         public Transform myTransform;
     public Transform Player;
     private GameObject MC;
     private MainCharacterScript script;
     // Use this for initialization
     void Start () {
         Player = GameObject.FindWithTag("MainCharacter").transform;
         myTransform = transform;
         MC = GameObject.FindWithTag ("MainCharacter");
         script = MC.GetComponent<MainCharacterScript>();
     }
     
     // Update is called once per frame
     void Update () {
             if(Player){
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(Player.position - myTransform.position), rotationSpeed*Time.deltaTime);
             var dist = Vector3.Distance(Player.position, transform.position);
                // Debug.Log ("Distance to player: " + dist);
             if(dist <= 100){ 
                 playercolor = script.GetPlayerColor();
                 if( playercolor % mycolor == 0 && dist > 10) { FollowPlayer (); }  
                 else if ( playercolor % mycolor == 1) { FleePlayer ();}
                 else {}
             }
         }
  }
     
 }


The problem happens in Update, where I set playercolor = script.GetPlayerColor(); at which point Unity throws a Null exception, as though script was not instanced in the start function. Any ideas / help would be greatly appreciated.

Comment
Add comment · Show 11
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 robertbu · Feb 24, 2013 at 05:01 AM 0
Share

I see nothing obviously wrong here. Have you put a Debug.Log() statement after line 19 to verify that 'script' is not null? Any chance you have two objects tagged with "$$anonymous$$ainCharacter", but only one has a $$anonymous$$ainCharacterScript?

avatar image LukaKotar · Feb 24, 2013 at 06:09 AM 0
Share

Does GetPlayerColor() return anything? Could you show us that function?

avatar image anonnewguy · Feb 24, 2013 at 06:19 AM 0
Share

Sorry about misusing the comment button. What would you like to see Luka$$anonymous$$otar? The function itself is posted above in $$anonymous$$ainCharacterScript, and it returns null as script is null.

avatar image LukaKotar · Feb 24, 2013 at 06:24 AM 0
Share

Oh, my bad. I missed it.

I don't see anything wrong with this either. $$anonymous$$aybe you could get the 'playercolor' directly?

 playercolor = script.playercolor;
avatar image theundergod · Feb 24, 2013 at 06:29 AM 1
Share

Three questions (and I know they seem rudimentary):

  1. Is this $$anonymous$$ainCharacterScript on the GameObject?

  2. Is $$anonymous$$ainCharacterScript on the root of the GameObject?

  3. Does your $$anonymous$$ainCharacter GameObject have any child objects and are they also tagged "$$anonymous$$ainCharacter"?

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by vladibo · Feb 24, 2013 at 06:30 PM

MC.GetComponent(); does not find what you are looking for

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

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is everything being set the same? Please Help! 1 Answer

EnemyAI C# Error 1 Answer

AI positioning in soccer game 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