Generating a script at runtime and attaching it to a gameobject after it is instantiated:

I need to generate a script file at runtime and attach it to a gameobject.

The reason is, is the gameobjects all have the same data but themselves are not, so I need a unique script to each model but is the same if that makes sense.

I can’t add them to the prefab because they are being downloaded. I’m assuming I can attach a scritp with just the name then grab it and rename it.?

I’m not sure what you are saying. You are downloading scripts or prefabs?

To add scripts, you use AddComponent.

However, if all you want is for example an enemy script that has hp, damage, stamina and similar variables but different values for those variables, you could still use a prefab and just have a way to initialize the values.

1 Like

I’m downloading a model at runtime…and will be downloading more at runtime. All the models have the same data that I want to display but its all specific to themselves. I need to have a placeholder script attached to the prefab to assume the download then be able to change the script name to be specific for that particular gameobject.

So this is impossible?

You aren’t expressing your needs in clear technical terms.

You can add a new Component to a GameObject using AddComponent, as Brathnann said above.

You can’t change a Component into a different type of Component, but you could remove one Component, add a different one, and change your variables to reference the new one. You could possibly do something clever with indirect references to make it “look like” it was substituted in-place.

You can’t compile new source code into a new class at runtime (at least not easily, and this is super dangerous).

You could probably figure out some abstract way of representing your data that would be compatible with multiple “models” so that you didn’t actually need to switch to a different Component, but I can’t say anything definitive about that since you haven’t explained any of your requirements.

1 Like

My comment above isn’t enough. I don’t know what else you would need to know.

@Antistone

You think a database base connection on the script to get each models data would be too much?

Question: Does the script exist at compile time? Or do you need to download the script at runtime?

If the script exists at compile time, its just as simple as AddComponent.

If the script needs to be downloaded at runtime, you are looking at reflection, which is a pain on the best of days. Normally if the answer if reflection, the question is wrong.

2 Likes

I didn’t think of that. Are you thinking there’s a way to create the script outside of Unity and then download it with the model?

I was going to put the script on the prefab with a database connection on it then once it’s instantiated, I will make it rename itself to reflect the data it is suppose to have through the database.

Or is that to much?

I feel like we have an XY problem here. What are you actually trying to accomplish?

My standard route would be to down load the model, then use add component to add a script to it, then set the data in the script (loaded from some sort of data store somewhere). This is pretty straight forward, as long as the script already exists in your build.

Creating an entirely new script at runtime has limited application. Its typically only useful for something like modding. Its a very advanced topic.

I can have the script attached to it.

So using a database is my only route?

I explained what I’m doing. If you need more information, you need to tell me what you need to know.

You can’t rename the script during runtime and honestly, why would you? You can have scripts inherit from the same base script if you needed, but unless there is a reason to (like normal enemy ability vs something a boss does different), this wouldn’t be super useful.

Users wouldn’t see the script name either, so that isn’t useful for game play purposes. You could add a field that would include the type of enemy it is. Even make it an enum if you wanted.

Otherwise, you should explain yourself with an example of what you are trying to accomplish, because it feels like nobody fully understands you or you don’t understand the answers being given, which could be because we don’t understand you.

Lets say I am just developing a model viewer. where you can import multiple models, not at once… but as you need to.

Each model has the same property names but those properties are different. So if you were to select the model you would see a properties window with its properties displayed accurately.

If I have a script attached to the prefab that is the wrapper for the import those properties are the same across all models. The properties are coming from a database.

I need each new imported model to be able to store its unique properties.

As far as loading the data, it can be stored however you want. Plain text, json, scriptableObject, database, backend service with key/value entities, it really doesn’t matter. You just need a way to load up the data and apply it to the object.

The way I would probably set it up is if I’m presenting a menu to the user, when they select a button to view a model that button can return an ID that lets me know what model to grab and what data to load up.

Yes, so connecting to database to get the properties or storing the properties in the file?

I have a script connected to the prefab to capture the properties when the model is imported, imported as in downloaded from a server by URL. The script grabs the properties of the model downloaded.

each model will have the same script. Would the same script be able to change to reflect each models different properties?

Or are you saying creating a class that I serialize that has the properties stored on it?

You can’t change the script itself. (can’t add another variable or remove a method for example during runtime)

But you can assign different data to it based on what model is loaded. (If you have a string variable modelName, you can change the value assigned to it.)

Even though that file attached will be on all other models in the scene?

Scripts aren’t shared…If you have 5 gameobjects in your scene, all with a script named “Model” on it, each one has it’s own copy. Changing the data on one doesn’t change the data on the others (ignoring the static variables for now).

If you use instantiate on a gameobject “Enemy” and you create 20 of them, you don’t want them sharing the same health pool. Creating an instance of a script is important for stuff like this.

2 Likes

You’re not picking up what I’m putting down

Because you aren’t explaining it well enough for what you are wanting. Several people have tried to answer you, if we’re all confused, then you’ll need to change how you explain what you want to do.

1 Like