- Home /
How do I set instance script parameters to a prefab?
I am attempting to make a game where the user can configure many instances of the same type of object/prefab but each instance in the game can have a different scale, color, texture, etc. How can I use the instantiate method the call my prefab to make a copy but then allow the script parameters to be modified on an instance based level rather than modifying all of the gameobjects of that type of prefab in the model? Just like when you make a class in C# with properties. You then instantiate that class multiple times and the values for properties on each of those instances may be different but the class itself is the same type of object. I'm a newbie so I know I am missing something here.
The Instantiate function returns the instance of the GameObject you just instantiated. store it in a variable and using the Send$$anonymous$$essage function , send the properties to your gameobject you want it to have, like your color, scale,etc. you need to write down a function in the script attached to your gameobject to receive the properties in the Send$$anonymous$$essage function. And using this function itself, change the properties of your gameobject. So these changes will be only on the gameobject, not on the prefab, and in essence, not on the complete set of other gameobjects sharing the prefab.
If this helps, mark it as solved and tick my comment as the answer.
This comes back to the whole concept of Object Oriented Program$$anonymous$$g (using classes). The whole idea of "Instantiating" something is that it is creating a separate "instance" of that class. $$anonymous$$eaning it is on it's own and separate from the class. Anything you change in that instance won't affect anything else that derived from the same class.
Answer by MagicoCreator · Apr 11, 2014 at 02:42 PM
Instantiate returns a reference to the instantiated object, just store it in a variable and change it's values
for example:
var MyInstance = Instantiate (MyPrefab, Vector3.zero, Quaternion.identity);
MyInstance.transform.localScale = Vector3(Random.Range(1,1.1),Random.Range(1,1.1),Random.Range(1,1.1));
if you want to access your custom scripts you can get them with GetComponent:
var MyInstance = Instantiate (MyPrefab, Vector3.zero, Quaternion.identity);
var MyScript = MyInstance.GetComponent(MyScriptName);
MyScript.MyValue = 1;
Your answer
Follow this Question
Related Questions
Why can't I undo changes to a prefab? (or to an instance of it after I apply the changes) 1 Answer
Prefab Canvas's buttons arn't working in different scene 4 Answers
How to undo prefab Apply? 0 Answers
I need to move a clone of a prefab to a specific position after it instantiates 1 Answer
How do I get the Player's Health Script as Component? 1 Answer