dev-notes/dotnet/unity/prefabs-instantiation.md

15 lines
625 B
Markdown
Raw Normal View History

2021-01-31 11:05:37 +01:00
# Prefabs
2021-09-20 19:35:32 +02:00
Prefabs are a blueprint for GameObjects and any change made to the prefab is inherited by all it's instances.
2021-01-31 11:05:37 +01:00
2021-09-20 19:35:32 +02:00
## Script Instantiation
2021-01-31 11:05:37 +01:00
```cs
public GameObject prefab; // reference to the prefab
// instantiate a game GameObject from the prefab with specified position and rotation (rotation must be a quaternion)
GameObject newInstance = (GameObject) Instantiate(prefab, positionVector3, Quaternion.Euler(rotationVector3)); // instantiate prefab and get reference to instance
// Instance returns a object and since a GameObject was passed to Instantiate() the returned object can be casted to a GameObject
```