Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't use float values in Vector3

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading