Script can't be added to

i’ve wrote two scripts for handling flocking behaviour. Both of the script’s have no errors. First i had only the problem that one script could not be attached to the GameObject for whatever reason. The compiler doen’t throw any errors. I don’t get what the Problem is and what should i do now. I’ve thought maybe the naming is a problem and tried to rename file and classes but that didn’t worked and i’ve reverted the namings again, but now it seems like no script can be attached at all anymore… I don’t know what i should do. Do you guys have any suggestion?
image.png

Original Post on Discord

by user 945986115150696489

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

Hello Madweb93,

Components that cannot be attached to GameObjects can be caused by different issues. Here are some steps you can take to troubleshoot and potentially solve the problem:

  1. Ensure Components are Registered: Make sure that your components are correctly registered by using the @registerType decorator or manually adding them to the _TypeStore. You can check (see the documentation) for how to use TypeStore.

  2. Naming Conventions: Confirm that your classes and files follow proper naming conventions and that their names do not conflict with built-in classes or interfaces (see the documentation).

  3. Compiler Checks: Although you mentioned that there are no errors, it’s worth double-checking that all your scripts compile without errors and that the types used in the scripts are not causing issues with the compiler.

  4. Debugging Mode: Check if you’re in debug mode using the isDebugMode() function which could help with additional logging and error messages (see the documentation).

  5. Component Lifecycle: Verify that all lifecycle methods (awake, start, etc.) are implemented correctly and that there are no runtime errors within these methods.

  6. Check the Console: Look at the browser console for any warnings or errors that might give additional information about the issue.

  7. Dependencies and Imports: Ensure that you aren’t importing the entire namespaces and only importing the types that you use (see the documentation).

If you have adhered to all the above suggestions and the issue persists, there might be a more specific problem with Needle Engine or your project setup that needs a closer look by examining the scripts and the exact way you are trying to attach them to GameObjects.

thread_ZnkQh3IngMUcHG3wvt5QAtYV

Please try these suggestions, and let me know if you need any further assistance.

1 messages left GPT 4
thread_ZnkQh3IngMUcHG3wvt5QAtYV

Did you rename the generated .cs files or are these non-generated .cs files?

noo i didn’t renamed them, but i found out that the generated code has some missing types and it also has Problems when i use Math.random in my typescript class. It seems that Math.random() won#t be converted to the UnityEngine.Random.Range function. I fixed that temperarly in the auto generated cs-file but now i try to figur out what i am missing in my typescript-file

by user 945986115150696489

it seems that types are not correctly auto generated

by user 945986115150696489

Here is what I found for you :brain: Discord

Can you show the code for that? Not exactly sure what you mean

Madweb93, the TypeScript to C# Component Compiler in Needle Engine relies on specific annotations to control the behavior of C# code generation from TypeScript. If types are not being correctly autogenerated, please ensure that:

  1. The TypeScript types are explicitly declared for your fields. For example, myField: number = 5. Implicitly typed variables will not be correctly translated.
  2. If necessary, use the // @type annotation to explicitly specify the C# field type.

If the generated stub components don’t align with your expectations, you can extend them using partial classes within the same namespace, so custom functionality doesn’t get overwritten with each compilation. Below is an example of how to use these annotations:

Typescript input:

import { AssetReference, Behaviour, serializable } from "@needle-tools/engine";
import { Object3D } from "three";

export class MyCustomComponent extends Behaviour {
    @serializable()
    myFloatValue: number = 42;

    @serializable(Object3D)
    myOtherObject?: Object3D;

    @serializable(AssetReference)
    prefabs: AssetReference[] = [];

    start() {
        this.sayHello();
    }

    private sayHello() {
        console.log("Hello World", this);
    }
}

Extend the generated C# component (make sure namespace and class name match):

using UnityEditor;

// your custom extensions go here - either above or below the NEEDLE_CODEGEN blocks

Finally, remember to have a Component Generator on the GameObject with your ExportInfo component for the generation to occur automatically when scripts are added to the src/scripts folder (see the documentation).

1 messages left GPT 4
thread_8uAKkj64FRLx7L1OnBRJzRwb

the Problem of not converted types are specifically in functions. how do i do fix the types in a parameter?

by user 945986115150696489

To fix the types of parameters in functions, use the @type attribute to explicitly specify the type of the parameter in your TypeScript methods.

For example:

export declare function myFunction(param1: string, param2: number): void;
// Use the @type attribute to specify the type explicitly
// e.g., @type string

By using these annotations, you can ensure that the TypeScript to C# Component Compiler correctly translates the types during the conversion process.

0 messages left GPT 3.5
thread_8uAKkj64FRLx7L1OnBRJzRwb

i don’t get it quite. Here for example:
separation(delta: number, neighbours: FlockUnit, range: number = 30) { … }

FlockUnit is the current class. how do i write that ?

by user 945986115150696489

You have reached your limit of 2 messages… please try again in a few hours

that is my FlockUnit.ts
FlockUnit.ts

by user 945986115150696489

And this is what is generated. here are the types missing and also Math.random() is used although unity doesn#t now that function.

Some “problem”-parameters are for example:

public void separation(float @delta, [] @neighbours, float @range){}
    public void alignment(float @delta, [] @neighbours, float @range){}
    public void cohesion(float @delta, [] @neighbours, float @range){}

FlockUnit.cs

by user 945986115150696489

And here i fixed the auto-generated code
FlockUnit_fix.cs

by user 945986115150696489

I think easiest if above them you add // @nonSerialized to skip generation of them
Like this:

// @nonSerialized
updateMovement(delta: number, neighbours: FlockUnit[], obstacles: any[]) {
...

okey that worked :smiling_face_with_three_hearts:

by user 945986115150696489