Hi,
I know that there are like milions of topics with very similiar question but still I haven’t been succesfull with the solution.
The problem:
Having two classes, one is PATH class, which has public array of Vector2s.
The other is HERO class, which would like to access PATH’s array and use that public array as some sort of “navigation”.
The problem is, that I’m just not able to access PATH’s array. I’m able to access some variables but not an array.
Code:
public class path_script : MonoBehaviour {
public Vector2[] points;
void Awake()
{//filling points array with Vector2s...
CatmullRom(((EdgeCollider2D)(this.collider2D)).points,out points,5);
}
}
and the hero class
public class hero : MonoBehaviour {
private path_script script_ref;
private Transform _transform;
private bool isset = false;
private Vector2[] path_points;
void Start () {
_transform = this.transform;
}
void Update () {
if ( !isset ) return;
// throwing "Object reference not set to an instance of an object"
_transform.localPosition = Vector2.MoveTowards( _transform.localPosition,path_points[path_step],Time.deltaTime * speed);
path_step++;
if ( path_step == path_points.Length - 1 )
path_step--;
}
void OnTriggerEnter2D(Collider2D col)
{
if ( col.name == "path")
{
script_ref = col.gameObject.GetComponent<path_script>();
path_points = script_ref.points;
isset = true;
}
}
}
I just don’t understand why it doesn’t work. I thought that with this
script_ref = col.gameObject.GetComponent<path_script>();
path_points = script_ref.points;
I have then reference to path_script’s array and I can access to that as I want to
EDIT
I’m sorry, I forget to paste a piece of code in hero’s script where I assign _transform. I edited it, now it just be more clear what is wrong
reference to Vector2 points array is null and thats why it throws “Object reference not set to an instance of an object”. That’s the problem I’m unable to solve and that is what I mean “can’t acces other script’s array”.
And why is points array type of object and not Vector2 ?
EDIT2:
Basically I can’t get that reference ( is null ), but it should be possible right?