#I m trying to do a super simple waypoint
1 messages · Page 1 of 1 (latest)
Code here too, because embeds are scary apparently.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WaypointManager : MonoBehaviour
{
public Image MarkerImage;
public List<Image> Markers; //List to hold all markers
public List<GameObject> Targets; //List to hold all target objects
// Update is called once per frame
void Update()
{
foreach (GameObject Target in Targets)
{
foreach (Image Marker in Markers)
{
Marker.transform.position = Camera.main.WorldToScreenPoint(Target.transform.position); //<<< NullReference for object here
}
}
}
}
The first question is, have you assigned anything to those lists in the inspector?
Yep 🙂
I tried with just the "Targets" list, it works fine and returns the position, but I get the NullReference when I nest the second for each
Do you have a camera with the MainCamera tag?
Search WaypointManager in the hierarchy and make sure you don't have another one lying around that hasn't assigned anything to the list.
I'm sure I don't have another, kept the scene tidy to eliminate that... The camera doesn't have a MainCamera tag, but I have used that method before for a sort of camera switch system and it worked fine, should I add the tag?
Camera.main targets whatever camera has that tag. So if none do, it won't know what camera you're referring to.