- Home /
How to change a prefab position during play mode
Hello Masters and Gurus.
I'm practising Unity with Flappy Bird demo. I want to adjust the gate on fly so it will get harder to pass them. The gate (columns) is a prefab. I have tried different methods and leave uncommented the one which doesn't give any errors and also change the position of the prefab but not playing columns. I also cut unnecessary parts like lower column away.
Could you please assist me? How to change prefab position while playing?
public class GameControlScript : MonoBehaviour
{
public Transform upperGate;
public float upperPosition = 3f;
//Try 1: Also tried with Transform.position. Which one is correct?
/*
Vector3 upper = transform.position;
upper.y = upperPosition;
*/
void Update()
{
//Also tried with different level, but that was a way too complicated
/* if (score >= 5)
{
Application.LoadLevel("level-2");
} */
if (score >= 5 )
{
//upperGate.position = upper; //Try 1
upperGate.position = new Vector3 (0, upperPosition, 0);
}
}
}
Comment
Answer by Gamevara · Sep 28, 2015 at 12:02 PM
Hi,
I managed to solve the problem. Instead of try to change prefab I created different prefabs and changing them during the game. Here is how I created it.
using UnityEngine;
using System.Collections;
public class ColumnSpawnScript : MonoBehaviour
{
public GameObject startPrefab; //the column game object: start
public GameObject level2Prefab; //the column game object: level 2
public GameObject level3Prefab; //the column game object: level 3
public int columnPoolSize = 5; //how many columns to keep on standby
public float spawnRate = 3f; //how quickly columns spawn
public float columnMin = -1f; //minimum y value of the column position
public float columnMax = 3.5f; //maximum y value of the column position
GameObject[] columns; //collection of pooled columns
int currentColumn = 0; //index of the current column in the collection
GameObject columnPrefab;
void Start()
{
columnPrefab = startPrefab;
//deactivate level2 and level3 prefab
level2Prefab.SetActive (false);
level3Prefab.SetActive (false);
setPool ();
//starts our function in charge of spawning the columns in the playable area
StartCoroutine ("SpawnLoop");
}
//removed the standard stuff here.
public void ChangeLevel (int score)
{
if (score < 2)
{
columnPrefab = startPrefab;
}
else if (score >= 2 && score < 9)
{
level2Prefab.SetActive(true);
columnPrefab = level2Prefab;
startPrefab.SetActive(false);
setPool ();
}
else if (score >= 9)
{
level3Prefab.SetActive(true);
columnPrefab = level3Prefab;
level2Prefab.SetActive(false);
setPool ();
}
}
void setPool ()
{
//initialize the columns collection
columns = new GameObject[columnPoolSize];
//loop through the collection and create the individual columns
for(int i = 0; i < columnPoolSize; i++)
{
//note that columns will have the exact position and rotation of the prefab asset.
//this is because we did not specify the position and rotation in the
//Instantiate() method call
columns[i] = (GameObject)Instantiate(columnPrefab);
}
}
}