#I m trying to do a super simple waypoint

1 messages · Page 1 of 1 (latest)

unborn latch
#

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
            }
        }
    }
}
vague tiger
#

The first question is, have you assigned anything to those lists in the inspector?

unborn latch
#

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

vague tiger
#

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.

unborn latch
#

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?

vague tiger
#

Camera.main targets whatever camera has that tag. So if none do, it won't know what camera you're referring to.

unborn latch
#

Oh that fixed it! Now just to fix the image going to the transform position instead of away somewhere since my targets aren't static. 😛 Thanks

#

Also can I ask, is it seen as wrong to nest for each loops like this? Is there a better alternative?

vague tiger
#

Well, you're essentially moving every marker to every waypoint. Which will basically snap them all to the last one only.

#

So in this case, you probably don't want to nest them.