- Home /
The question is answered, right answer was accepted
Null Reference Problem Code+pic (SOLVED)
How can this give me a null reference problem:
function Update () {
var AC : AttackComponent = GetComponent(AttackComponent);
AC.findNearEnemy();
}
The AttackComponent script is already put on my gameobject which is called "smallUnit":
Moderator Note: Remember that you can also edit your questions when you need to give more details. I thought the pictures would stay on the server after I deleted the answer. They didn't. -Jamora
EDIT:
First of all...I am so amazed of this community, wow thanks for the replies!
Back to topic: My gameobject has 3 important scripts on it:
AttackComponent(instantiated as AC)
MoveComponent(instantiated as MC)
UnitController(instantiated as UC).
The last stated is the "main" script of the gameobject which works as a bridge between the two first stated, so they can call different functions through the UnitController. Anyway as you can see, the three scripts is attatched to the gameobject:
Broken down to this scenario, my UnitController has this:
var AC :AttackComponent;
var MC :MoveComponent;
function Awake(){
AC = GetComponent(AttackComponent);
MC = GetComponent(MoveComponent);
}
function Update () {
AC.findNearEnemy();
}
My AttackComponent has (broken down for this scenario) this:
var UC : UnitController;
function Awake(){
UC = GetComponent(UnitController);
}
function findNearEnemy(){
//nothing yet, just a dummy function
}
I think it is a problem with the AC.findNearEnemy(). My MC in UnitController doesn't give me any problems, so I am on unknown ground for my sake :( The error message i get:
Answer by skfs · Oct 10, 2013 at 04:33 PM
I FOUND IT...I FOUND IT.... Sorry for taking anyones time....after some time, I decided to look at the other gameobjects I have in the scene. And here I found, that I have by a mistake applied the UnitController to a gameobject with no such need, and therefore when the script repeatedly called the AC.findEnemyUnit() on an object that didnt have an AttackComponent script on it..it puked the error.. Sorry for anything, and thanks for everything :)
This brings up an interesting Unity functionality.
Right Click a script in Project Explorer and click
Find References in Scene :P
$$anonymous$$ost useful yet Under-Used!
Thank you very much. I will as a fact never forget that again :)
Answer by meat5000 · Oct 09, 2013 at 07:58 PM
Drag the relevant objects in to the places in the script where you see 'None'
Oh sorry if you misunderstood me. I have a script called "unitController" this script needs to call findNearEnemy() in another script, which is already on the gameobject, called AttackComponent. So the None spaces should be filled out when game starts :) But i still get a null reference ?
The picture is used to illustrate, that i have a script called AttackComponent on my gameobject.
Try filling them with dummys. If they get populated anyway it may remove the NullRefs. If not we'll keep debugging.
You will only get a null reference with the code you outline if the current game object does not have an AttackComponent script attached. Are you sure AttachComponent is attached to the same game object as the script above?
As clunk47 stated, please post the full error code. At a glance, I'm guessing the NRE is co$$anonymous$$g from your call to AC.findNearEnemy(). You mentioned that findNearEnemy is controlled by a script called "unitController", and there is a spot in your inspector called "UC" which is a "unitController" that is unassigned. That could be causing your error. Where is the assignment to this "UC" variable being made? Unity won't scan your project or scene for a script to fill your variables for you.