2dkot
1
I have script where instance of class created. This class call function from different class. Well here is code:
Script A:
var antWorker:AntWorker;
function Start(){
antWorker = new AntWorker(gameObject);
}
function Update() {
antWorker.patrolMove(movePoints*);*
}
class AntWorker:
class AntWorker{
var currentObject:GameObject;
var patrolSpeed:int = 20;
var gravity:int = 20;
var botAction:botActions;
*public function AntWorker(currentObject:GameObject){ *
this.currentObject = currentObject;
botAction = new botActions();
}
}
Class botActions:
class botActions{
function simMove(currentObject:GameObject,mytarget:Vector3,speed:int,gravity:int){
-
var currentSpeed:int = speed;*
-
var currentGravity:int = gravity;*
-
var currentPos = currentObject.transform.position;*
-
var currentRot = currentObject.transform.rotation;*
-
var controller : CharacterController = currentObject.GetComponent(CharacterController);*
-
if (controller.isGrounded) {*
-
moveDirection = mytarget-currentPos;*
-
moveDirection.y = 0; // ignore vertical distance before normalization*
_ moveDirection = moveDirection.normalized * currentSpeed;_
_ moveDirection.y -= currentGravity * Time.deltaTime;_
_ controller.Move(moveDirection * Time.deltaTime);_
- currentObject.transform.rotation = getTerrainRot(mytarget,currentPos,currentRot);*
}
}
But, if I call function of botActions in Script A not through class AntWorkerm, everything works fine.I suspect that something wrong with constructor (( but can’t find where.
Exception:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
antWorker1.Update () (at Assets/gScripts/classes/Animal/Insect/antWorker1.js:53)
Thanks
“Object reference not set to an instance of an object” means that you are trying to use a variable that is null (empty) instead a variable pointing to an active object.
What you need is to assign the variable correct.
From your constructor, I think you make the error when you have the same variable name for both parameter to the constructor + using it as class variable. Perhaps this version will do it?:
class AntWorker
class AntWorker
{
var goParent:GameObject;
var patrolSpeed:int = 20;
var gravity:int = 20;
var botAction:botActions;
public function AntWorker(goCaller:GameObject)
{
goParent = goCaller;
botAction = new botActions();
}
function patrolMove(target:Vector3)
{
botAction.simMove(currentObject,target,patrolSpeed,gravity);
}
}
If thats not the problem, then try using Debug.Log(variablename) to see if it writes Object or Null in the Console when it hits the line.
usually I write them like this:
Debug.Log("Gameobject Enemy: " + goEnemy)
Once I find a line that says “null” instead of the object, I know where it goes wrong.