- Home /
public static transform waypoints problem
My code checks the character's tag and then assigns a time-scheduled route of waypoints to follow. I tested this with a few characters and a few waypoints and it worked perfectly.
To be clear: My "game clock" would say it's 5pm, and the character would leave work for the bar. Then at 8pm they'd go home for dinner. At midnight, they'd sleep. At 8am, they'd wake up and then at 9am they'd go to work and it would keep repeating.
Now I want to use more waypoints and characters.
But the idea of dragging 50 waypoint game objects to each character so that they all know how to navigate the map seems silly.
My solution was to have a single game object containing the reference to all of the waypoints, as public static Transform waypoint01, public static Transform waypoint02, etc.
I thought this was pretty brilliant on my part (as a total novice) but it doesn't work. I get a NullReferenceException error: object reference not set to an instance of an object, at line 18, which is where I set the initial value before letting each character overwrite it depending on their schedule.
Line 18: Vector3 waypoint = WaypointLocations.waypoint01.position;
I did some searching around and it seems like my problem is that I can't (or shouldn't) use public static transforms the way I'm doing. Does anyone know either a quick fix to make what I have work, or know what a more proper solution is?
As an alternative, I tried declaring the script from the other object (public WaypointLocations WP), then using Vector3 waypoint = WP.waypoint01.position; and, while it compiles, I am getting the same error and it's not working.
Have you considered using a static List ins$$anonymous$$d?
Have you made the WaypointLocations class itself static
, not just the waypoint members? Also, woyuls strongly recommend an array of transforms rather than individually named waypoint1, waypoint2 etc.
Your answer
Follow this Question
Related Questions
transform.position any suggestions? 2 Answers
transform.position.x brings up error 1 Answer
Linecast blocked by colliders not actually in the way? 0 Answers
Newbie question 2d positioning and moving 2 Answers
Object Boundaries C# help please 1 Answer