I’m Instantiating ‘Cubes’ in my scene using a script called ‘InstatiateScript’. It works like a charm.
Now I want all the positions of these ‘Cubes’ instantiated stored in a list to do some 3D-pathfinding when needed.
To start I created a simple script called ‘PathfindingScript’ with a method to add locations.
using System.Collections.Generic;
using UnityEngine;
public class PathfindingScript : MonoBehaviour
{
public List<Vector3> PfNodelist = new List<Vector3>();
public void AddNodeToList(Vector3 NodeLocation)
{
PfNodelist.Add(NodeLocation);
}
}
This Is what already had. This is a method from my ‘InstantiateScript’ I created to instatiate a cube at the ‘InstLoc’ (Vector3).
I only added the PathfindingScript.AddNodeToList(InstLoc);
And the “public PathfindingScript PathfindingScript;” inside the "public class InstantiateScript : MonoBehaviour" … obviously.
This is the error message:
The ‘62’ is line ‘8’ in the above code example.
NullReferenceException: Object reference not set to an instance of an object InstantiateScript.CreateNewCube (UnityEngine.Vector3 InstLoc) (at Assets/InstantiateScript.cs:62)
You say that the script is inside the same file, or what? When you say “inside the…” what did you mean?
You probably should have a reference to another class instance, get the component first in start/have it assigned to a public field manually.
Looks like you are trying to call the class name there on line 8, that capitalized PathFindingScript. Where’s your instance of that class you try to access?
Here’s a few pointers to the right direction:
Create your classes to different files with their class name.
In your class that wants to add a cube, you need to include the reference to the class that contains the method you call.
If your class was called ‘FindPath’, you could do it like this:
// Add this script to your object as a compoment:
FindPath findPath;
You need to then have a script assigned to your object in Inspector, like this:
Then, in Start:
// Get the reference to the PathFinding instance
findPath = GetComponent<FindPath>();
Now, if the component exists on the object, it is cached to that ‘findPath’ variable. Notice the lower case in name.
Now, when you call it in your method (or wherever you call it):
// Add to the list in pathFinding
findPath.AddNode(position);
You call the method inside the instance of that PathFinding class, which is the ‘pathFinding’. It contains the created list where you can add stuff and the whole instance actually exists.
Thnx for the trouble, I forgot to ‘drag’ the object where the script resides into the inspector “Pathfinding Script” field … .
Tried it and it worked.
Oops, sorry.
But I don’t like that method at all. Always having to look in two places to see if everything connects …
I like your example more, all information in one place. Still have to wrap my mind around logic of it a bit, but I’m getting there.
That happens quite often to everyone using Unity I bet. I’ve sometimes been fixing an error for an hour or two, only to notice that some object somewhere is missing or a value in UI is zero…especially nasty if there’s no error. But that’s what Debug.Log prints are for.
Name your variables lower case so that you don’t confuse them with Class names, unless they are properties (get/set). Just a style thing but that’s how it’s in Microsoft C# reference manual.
You would reference your ‘FindPath’ in your current script, where you want to access it, like I showed in my example.
If you hand EnemyUnit script, then in there you would do it like this:
// Create a variable for that find path so that you save the reference
FindPath findPath;
// Get the reference to the PathFinding instance
// This would work if the script aka component is in the same game object as this script
findPath = GetComponent<FindPath>();
// To call the method in this current script:
findPath.AddNode(position);
This could be anywhere in your other scripts, as long as the fields are stated to be publicly available (GameObjects, values, pretty much anything).
So, to get that vector, you would just do:
// If you wanted to debug log that value, you could:
Debug.Log("position: " + findPath.PositionOfObject);
// Or make a reference to it locally in your current script:
Vector3 pos = findPath.positionOfObject;
Hope this makes sense better. You could just feed that value to something directly, like:
otherPos += findPath.positionOfObject;
Assuming otherPos was a Vector3 you have declared earlier.