How to deal with negative value that converted in to double not float ? - Discord Import

I wrote some value to adjust position of an object in typescript, but I could not define it with float because as far as I know they only have number. The Problem is that I am getting error for Vector3 in Unity that the value cannot be double for Vector3, could you please tell me how to deal with this?

Original Post on Discord

by user 151660356224286721

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

It appears you’re facing a type mismatch issue when adjusting the position of an object in TypeScript and having issues with the Unity Vector3 type expecting float values, whereas in TypeScript, you only use number types which translate to double in C#.

Typescript only has the number type, which can hold both integer and decimal numbers. On the other hand, Unity’s Vector3 structure expects float values specifically. number types in TypeScript may be translated into double precision floats when converting TypeScript to C#, leading to the error since Unity’s Vector3 does not accept double.

When you’re working with numeric values in TypeScript that are intended to be used with Unity’s Vector3, it’s best to ensure that these values are serialized and deserialized explicitly as floats. In TypeScript, you don’t need to worry about defining the numbers as floats, as there is no distinction at that level, but during serialization and deserialization, you should ensure that the process treats the numbers as float types compatible with the Unity Vector3.

For example, if you are using a TypeScript to Unity serialization tool, there may be annotations or comments you can add to specify that the TypeScript number should be treated as a float on the Unity side. If the tool provides a mechanism like that, you should use it.

Here’s a quick code snippet that would illustrate setting a position in TypeScript:

import { Vector3 } from "three";

// Set position with TypeScript number values
const myPosition: Vector3 = new Vector3(1, 2, 3);


Hi,. this looks like a bug in the component compiler - I’ll log it as an issue and will look into that. Until we have a fix I think you need to either use positive values for correct codegen OR you can mark the field with //@nonSerialized and add the field yourself manually outside of the partial c# class

Thank you for the answer ! :smile:

by user 151660356224286721

Another workaround would be to remove the default values and just set them in Unity in the inspector