I’m currently working on a Unity project where I want to instantiate a roadSection GameObject when my player object enters a trigger collider (tagged as "Trigger"). The instantiation requires setting the position using Vector3, and I need to use float values for accuracy.
However, when I try to use float values in new Vector3(9.5, 0, 10.5), I encounter an issue. Unity seems to reject the float values in this context.
How can I correctly instantiate the roadSection GameObject with precise float values for the position in Unity? Is there a different approach or method I should be using?
Here’s the relevant code snippet for reference:
public class SectionTrigger : MonoBehaviour
{
public GameObject roadSection;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Trigger"))
{
Instantiate(roadSection, new Vector3(9.5, 0, 10.5), Quaternion.identity);
}
}
}
I’m open to suggestions and virtual high-fives. Thanks for helping me sprinkle some floaty magic into my game! 🚀✨
>Solution :
9.0 is a double, not a float; try 9.0f and 10.5f instead; since 0 is naturally an int, it shouldn’t need a suffix (since an int can be assigned to a float), but it wouldn’t hurt to use 0f if that is clearer to the reader.