hi i’ve got this problem where I have a bunch of enemies using the same AI script so when 1 enemy does an attack the rest of the enemies do the attack regardless of their situation.
can someone tell me how I can avoid this? so far I’ve just been creating the same scripts but naming them differently but thats not going to cut it anymore
Also each enemy has a separate script that represents their weapon, so the weapon equiped becomes active when the static variable in the ai script tells it to.
How do I tell the weapon script to only check the scripts attached to the gameobject or parent rather than check every enemy script in the scene with the same name
Make them into prefabs.
Use gameObject.GetComponent to find the scripts for that game object.
and yes, don’t use statics unless you really only want one instance.
i’m trying ‘getcomponent’ but im getting this error
NullReferenceException: Object reference not set to an instance of an object.
I looked at the documentation but im not sure if I wrote this right.
function Update () {
var NewVariable: AIScript = gameObject.GetComponent(AIScript);
if(!NewVariable.BooleanVar){
//Do stuff
}
note ‘BooleanVar’ refers to a variable in the AIScript. The error is on the line where I write ‘if(!NewVariable.BooleanVar){’
how do I fix this?
also note this script is attached to the bone of the gameobject, ie the child is trying to access the variable from its parent.
EDIT:
I think I’ve found a solution I used this method
var gameobjectvariable: GameObject;
var newvariable: AIScript= gameobjectvariable.GetComponent(AIScript);
so far it works but I’m still getting an error message saying ‘NullReferenceException: Object reference not set to an instance of an object’
is there something im doing wrong or should look out for?
What it’s saying is it didn’t find the script. Are you sure it’s attached to the gameObject? Are both scripts components of the same gameObject. Will it always be calling this same script on this game object. If so, you can use getComponent in the start function to get a reference for a global variable, so you don’t have to call it every time in the update, or just drag a reference in the editor. GetComponent is generally too expensive to use in an update like that unless there is no other way. Something like:
var script:scriptname;
function Start(){
script = gameObject.GetComponent(scriptname);
}
function Update(){
if(script.boolVar) doSomething();
}
the ‘getcomponent’ is at function start, the script isn’t on the same object its on the parent. But where I i assigned the gameobject with the script I want to refer to in the inspector.
‘var gameobjectvariable: GameObject;’
‘var newvariable: AIScript= gameobjectvariable.GetComponent(AIScript);’
This method does work meaning its found the script but getting the long list of error messages annoys me
It sounds like some type of timing problem. Apparently the update functions for a little while on a null reference, and then there is one. You can just test for null first;
function Update(){
if(newvariable != null) {
if(newvaribale.boolvar) do something();
}
}
I thought the update wasn’t supposed to start until after the start function finished. My guess is the reference object is instantiated after this one. Maybe you could arrange the order in the tree. Don’t know if it uses that or not.
Otherwise you could try to change the script execution order:
sry I don’t havent put it in a function I just have it on the top of the page, when I place that line in function start it gives me an error saying unknown Identifier.
var GameObjectVar: GameObject;
var NewVar : EnemyAI = GameObjectVar.GetComponent(EnemyAI);
function Start () {
//var NewVar : EnemyAI = GameObjectVar.GetComponent(EnemyAI);
}
function Update () {
//var MKWeapon : MalphasK = gameObject.GetComponent(MalphasK);
//var MKWeapon : MalphasK = Weilder.GetComponent(MalphasK);
if(!NewVar.boolvar){
//Do stuff
}}
if I comment out the line
var NewVar : EnemyAI = GameObjectVar.GetComponent(EnemyAI);
and put it in the function start or function awake, I get an error at line 13 saying it does not recognize NewVar. So I left that line at the top. it works fine there but I still get the error message