- Home /
The question is answered, right answer was accepted
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.
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?
Does GetPlayerColor() return anything? Could you show us that function?
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.
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;
Three questions (and I know they seem rudimentary):
Is this $$anonymous$$ainCharacterScript on the GameObject?
Is $$anonymous$$ainCharacterScript on the root of the GameObject?
Does your $$anonymous$$ainCharacter GameObject have any child objects and are they also tagged "$$anonymous$$ainCharacter"?
Answer by vladibo · Feb 24, 2013 at 06:30 PM
MC.GetComponent(); does not find what you are looking for
Follow this Question
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