How can i get a script of a non instantiate prefab?
Hi,
I need change the values of a prefab before instantiate it. It's possible or i need to save N prefab with different values each one?
Because the prefab its the same, but i want modify his mass of rigidbody to create different gravities for each instantiate;
Thanks!
C#, Unity 2017.1.0f3
Answer by UnityCoach · Aug 16, 2017 at 05:52 PM
You can't modify a Prefab's values at runtime.
I guess you're getting those values from Awake or Start method.
You could call a custom "Init" method on your instantiated objects after instantiation.
To be honest, you'll probably have to rewrite part of your code. While you're at it, you could add some Object Pooling. This will lead you to adding custom Init (or OnEnable) initialisation.
Can i get instantiated prefab, change values on it and save to replace older prefab? thanks UnityCoach.
You can use PrefabUtility to modify/save Prefabs. But this will only be available in the Editor.
At runtime, Prefabs are nothing but serialised game objects.
I solved the problem with an update of Phisics.gravity. thanks
Answer by Camilohoyos16 · Aug 16, 2017 at 05:12 PM
The values of each prafabs will mantain in each instantiating. So, I dont see a problem if you change it after intantiated, but if you want to do what you say, you need a reference of prefeab in the script, like drag and drop in the inspector, and change the values that you mention and then instantiate that prefab. I dont so sure about the last solution.
I want to change the values before instantiating because the prefab is a bullet, and on instantiate i need know the mass and velocity to calculate gravity. So i can't modify values after instantiated it. Thanks CamiloHoyos16.
Your answer
Follow this Question
Related Questions
How can I in-script create gameobject from prefab? 2 Answers
Network.Instantiate passing parametrs to instantiated object, unassigned reference exception 0 Answers
Destroy a Prefab from an Array? (C#) 2 Answers
Instantiation of my GameObjects spell (from other script) 0 Answers
Instantiated Prefab won't instantiate with a script reference 1 Answer