- Home /
Instantiate prefab, add to list, destroy older prefab
Hi - I apologize that I am really new to C# and programming in general, so I'm sure I'm missing something obvious, but I have been working on this for a long time and it never works exactly as it's supposed to.
I am making a short path that replicates itself as the player moves along it. When the player gets to the edge of a block, a new block appears in front of them. If more than 3 blocks have been instantiated, the oldest block should be destroyed.
My code works for the first clone, but no matter how I do the parent-child, the trigger object either doesn't clone with the block, makes dozens of clones in a second ignoring the block limit, or makes more clones in the exact same place as the first clone.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Birth : MonoBehaviour {
public GameObject lego;
public int cloneLimit;
public List<GameObject> legos;
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("Player")) {
CheckCloneCount ();
}
}
void CheckCloneCount(){
if (legos.Count > cloneLimit) {
GameObject badClone = legos [1] as GameObject;
legos.Remove(badClone);
Destroy (badClone);
} else {
GameObject clone = Instantiate (lego, Vector3.forward * 2, Quaternion.Euler(-90, -90, 0)) as GameObject;
legos.Add (clone.gameObject);
}
}
}
Thank you for your time!
Answer by fdz_ · Dec 11, 2017 at 03:26 AM
Hi, how's it going? Before anything else, you wrote the whole post twice.
So, on one part you said:
makes dozens of clones in a second ignoring the block limit
Have you attached the script on more that one object? If the legos them self have it, that would explain it.
On other:
makes more clones in the exact same place as the first clone.
And this is the code for spawning:
GameObject clone = Instantiate ( lego, Vector3.forward * 2, Quaternion.Euler(-90, -90, 0) ) as GameObject;
The second parameter is the position, and well there is no code changing it. Vector3.forward
is always equal to (0, 0, 1)
, you are always multiplying it by 2 so you always get (0, 0, 2)
.
Maybe you wanted to do something like this:
else {
if ( legos.Count != 0 ) {// if list is not empty
Transform lastLegoTrans = legos[legos.Count-1].transform;// grab last lego's transform
Vector3 legoPos = lastLegoTrans.position;
Vector3 legoFwd = lastLegoTrans.forward; // where the lego is looking, test it out,
// maybe you don't need it and instead Vector3.forward could be use
Vector3 pos = legoPos + legoFwd * 2;
GameObject clone = Instantiate ( lego, pos, Quaternion.Euler(-90, -90, 0)) as GameObject;
legos.Add( clone.gameObject );
}
else {
GameObject clone = Instantiate ( lego, Vector3.zero, Quaternion.Euler(-90, -90, 0) ) as GameObject;
legos.Add( clone.gameObject );
}
}
Wow, embarrassing! Edited the post to ask only once.
Thanks! I made some small adjustments to the positioning of the prefabs and it totally works now! I really appreciate your help!
Out of curiosity, if I want to modify the script so that it works side to side as well as forward, is it better for the game to write new script for each direction or should I modify this script so that "forward" direction becomes relative to the player (?) ?
Thanks again! I'll keep playing with it.
$$anonymous$$mm... firstly you have to detect which direction the player is going, I don't know how you are handling the input of the player nor how you move the character, but it would be something like this:
Transform lastLegoTrans = legos[legos.Count-1].transform;// grab last lego's transform
Vectro3 dir = lastLegoTrans.forward;
if ( player_is_going_to_the_LEFT_of_the_lego ) {
dir = -lastLegoTrans.right;
}
else if ( player_is_going_to_the_RIGHT_of_the_lego ) {
dir = lastLegoTrans.right;
}
else if ( player_is_going_to_the_BAC$$anonymous$$_of_the_lego ) {
dir = -lastLegoTrans.forward;
}
Vector3 legoPos = lastLegoTrans.position;
Vector3 pos = legoPos + dir * 2;
GameObject clone = Instantiate ( lego, pos, Quaternion.Euler(-90, -90, 0)) as GameObject;
legos.Add( clone.gameObject );
If it's necessary, you can combine directions:
Vector3 dir = trans.right + trans.forward;
Oh... and maybe you could have various Colliders as triggers for checking where the player is moving, I mean, it totally depends in how you are doing the other stuff.