- Home /
Null Scripts
In an attempt to have some form of AStar pathfinding I have my enemies each referencing an AStar script, however, no matter how I initialise it, the referenced script always seems to be null, I have tried attaching it to the prefab and using:
AStar aStar = getComponent();
I've also tried
Astar astar = new AStar();
but no matter what I do, the variable remains null, it's infuriating because I swear I've done this before, but it just doesn't seem to be working this time, can anyone help me out here? Am I doing something stupid?
I'm using C# by the way.
Those code segments are not even syntactically correct. What did you actually try, and how did you know the result was null? An error message of some kind?
Wow, ok my bad, I must've just zoned out whilst typing that.
the first was Astar aStar = getComponent();
the second I believe was how I actually did it, I copied it from an A Star tutorial and it seemed to work for the guy.
ok so evidently I did do it right, but unity answers doesn't like greater than and less than symbols so I'll use commas ins$$anonymous$$d
Astar aStar = getComponent,AStar,();
You need to format as code (the 101010 button, or indent 4 spaces), then they show up.
Right, so what I entered was
Astar astar = getComponent<AStar>();
Answer by aldonaletto · Sep 02, 2011 at 04:27 AM
If the script is attached to the same object, you should use:
AStar astar = GetComponent<AStar>();But if the script is attached to another object, you need some reference to it. Supposing other is the object transform or collider:
AStar astar = other.GetComponent<AStar>();
that's the problem, I've done that, but it's still returning as null.
What exactly are you trying to do? Usually we have to get a script via GetComponent when it's part of a different object - if you want to call some function or alter some variable in your enemies, for instance. In this case, you need a reference - collider, transform, gameObject, whatever - to the target object, like above. Another possibility is when you have several scripts attached to the same object, and one script must alter/call something in another script - there's no need for a reference, in this case.
The script is attached to the same prefab calling it, esentially I'm trying to call on variables and functions stored within the script to use in another script tied to the same prefab, I've been using
AStar astar = GetComponent<AStar>();
but whenever I try to use the astar variable to call functions and references variables, it's always null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add multiple values to single variable(or something else)? 0 Answers
How to instantiate an object with scripts? 1 Answer
How to stop loading of a scene that's loading using SceneManager.LoadSceneAsync ? 1 Answer
What does this statement construction mean?: if (null != anim) 1 Answer
NullReferenceException MaskableGraphic.UpdateCull() upon Loading Player Data. 0 Answers