- Home /
How to instantiate prefabs with scripts attached?
I've a grid that spawns two different colour cubes onto it. Now I am trying to swap out the cubes for prefabs which I've attached scripts to but when the prefabs spawn the scripts are gone. I think I might be supposed to use Gameobjects for the prefabs but I'm not really sure what I'm doing.
EDIT: I know I can set the pink and yellow Cube to prefabs that are already in the scene and the scripts will work but I'd like to be able to instantiate prefabs that arent in the scene.
Any help would be much appreciated!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MapGenerator : MonoBehaviour {
public Transform tilePrefab;
public Transform pinkCube;
public Transform yellowCube;
public Vector2 mapSize;
public Vector2 zeroZero;
[Range(0,1)]
public float outlinePercent;
List<Coord> allTileCoords;
Queue<Coord> shuffledTileCoords;
public int seed = 10;
void Start() {
GenerateMap ();
}
public void GenerateMap() {
zeroZero = mapSize/2;
allTileCoords = new List<Coord> ();
for (int x = 0; x < mapSize.x; x ++) {
for (int y = 0; y < mapSize.y; y ++) {
allTileCoords.Add(new Coord(x,y));
}
}
shuffledTileCoords = new Queue<Coord> (Utility.ShuffleArray (allTileCoords.ToArray (), seed));
string holderName = "Generated Map";
if (transform.Find (holderName)) {
DestroyImmediate (transform.Find (holderName).gameObject);
}
Transform mapHolder = new GameObject (holderName).transform;
mapHolder.parent = transform;
for (int x = 0; x < mapSize.x; x ++) {
for (int y = 0; y < mapSize.y; y ++) {
Vector3 tilePosition = CoordToPosition(x,y);
Transform newTile = Instantiate (tilePrefab, tilePosition, Quaternion.Euler (Vector3.right * 90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent);
newTile.parent = mapHolder;
}
}
//Pinks
int pinkCount = 1;
for (int i =0; i < pinkCount; i ++) {
Coord randomCoord = GetRandomCoord();
Vector3 obstaclePosition = CoordToPosition(randomCoord.x,randomCoord.y);
Transform newObstacle = Instantiate(pinkCube, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
newObstacle.parent = mapHolder;
}
//Yellows
int yellowCount = 5;
for (int i =0; i < yellowCount; i ++) {
Coord randomCoord = GetRandomCoord();
Vector3 obstaclePosition = CoordToPosition(randomCoord.x,randomCoord.y);
Transform newObstacle = Instantiate(yellowCube, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
newObstacle.parent = mapHolder;
}
}
Vector3 CoordToPosition(int x, int y) {
return new Vector3 (-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
}
public Coord GetRandomCoord() {
Coord randomCoord = shuffledTileCoords.Dequeue ();
shuffledTileCoords.Enqueue (randomCoord);
return randomCoord;
}
public struct Coord {
public int x;
public int y;
public Coord(int _x, int _y) {
x = _x;
y = _y;
}
}
}
scripts are attached to gameobjects... not transforms. try useing GameObject variables for storing and spawning your prefabs.
Answer by sath · May 02, 2020 at 07:41 AM
If the prefabs have been set up correctly this shouldn't be happening. But there is another option for that case. You can add a script to the prefab upon instantiation.
Transform newObstacle = Instantiate(pinkCube, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
Script missingScript = newObstacle.gameObject.AddComponent<Script>();
//do something with this script
//missingScript.SetCubeColor(Color.green);
You were right I wasn't using the prefab right, working now. The ability to attach a script after instantiation is also good to know. Thank you.
Your answer
Follow this Question
Related Questions
instantiated GameObject Prefab jumps in air after instantiate 1 Answer
How to hide a character prefab 1 Answer
Reset script to prefab values 0 Answers
Spawn At Spawnpoint 2 Answers
Hiding gameobjects in hierarchy make it less expensive ? 0 Answers