- Home /
How can I change the instance of a prefab instead of the prefab itself?
I have a powerup (prefab) that changes the scale of a gameObject (prefab). However, my code is altering the prefab's values and not the instance of the prefab.
public class WiderPowerUp : PowerUp
{
[SerializeField] Paddle paddle; //Game Object being scaled
protected override void TriggerPowerUp()
{
Vector3 scale = paddle.gameObject.transform.localScale;
scale.x += 0.1f;
paddle.gameObject.transform.localScale = scale;
}
}
Is it possible to pass the local instance of a prefab to my script via [SerializeField], or do I have to use GameObject.Find()?
Answer by Acdia · Jul 21, 2020 at 05:55 AM
You can, but only if the gameobject is in the scene at the beginning already or (as far as I know) on editing the prefab (double click and make your references). But if your skript is on the gameobject you want, just use gameobject
or for the transform transform
for example: Vector3 scale = gameobject.transform.localScale;
or even easier: Vector3 scale = transform.localScale;
Thank you for taking the time to reply.
You can, but only if the gameobject is in the scene at the beginning already
To clarify, do you mean if the power up is in the scene already?
The paddle has its own gameobject. The powerup is its own gameobject with its own script attached (shown above), but is only instantiated at certain times.
Yes, as far as I know. But it should be possible by editing the prefab. So Evers powerup has a paddle, right? There is also GetComponentInChildren. But as said, you should be abled to make your connections by editing the prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003629im_/https://answers.unity.com/themes/thub/images/avi.jpg)