- Home /
How to instantiate different prefab on the end of a chain of prefabs?
Hello, sorry I am a big noob at this stuff, only recently started. But I'm essentially making a snake-dragon with a head, multiple body segments, and a tail (that I want to appear). The body pieces all follow the prefab that is in front of it(like a conga line), starting the instantiation at the head. I want this chain of pieces to end with the Tail prefab, but I'm not sure how to do it. I've already tried doing something...but it doesn't work(the script with SerpentTailMovement). (Please let me know if I need to give more details. Also, lemme know if you can see the code attached, I've never asked a question on this website before) Thanks! <3
the commented out code is what I tried (that isn't working) in the serpenthead script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SerpentHead : MonoBehaviour
{
public SerpentSegmentMovement segmentPrefab;
public int numSegments = 3;
// public SerpentTailMovement tailPrefab;
//public int numTail = 1;
void Start()
{
//go through and generate numsegments
//lots of prefabs. Call the init method
//on each one, passing the previous gameobject
//as the leader.
GameObject leader = gameObject; //start with the head
for (int i = 0; i < numSegments; i++)
{
SerpentSegmentMovement s = Instantiate(segmentPrefab, transform.position, Quaternion.identity);
s.Init(leader);
leader = s.gameObject;
// SerpentTailMovement t = Instantiate(tailPrefab, transform.position, Quaternion.identity);
// t.Init(leader);
//leader = t.gameObject;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SerpentSegmentMovement : MonoBehaviour
{
public GameObject leader;
public float followSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (leader != null)
{
transform.position = Vector3.Lerp(transform.position, leader.transform.position, Time.deltaTime * followSpeed);
transform.LookAt(leader.transform.position, Vector3.up);
}
}
public void Init(GameObject previous)
{
leader = previous;
}
I duplicated some code in the serpent head script and gave the tail prefab the same script as the body segment prefab, and made its leader the body instead of the head. now I'm not sure what to do? explanations and hints are fine as well as I want to learn (but I also wouldn't mind a bit o script) ;)
image of my unity ![alt text][1] [1]: /storage/temp/170735-etetet.jpg
Answer by jackmw94 · Nov 10, 2020 at 10:43 AM
Hiya
I think your problem here was that you were adding a tail for each segment! I expect you're only planning on having a single tail and therefore it shouldn't be inside the for-loop. If you take those 3 commented lines and put them outside the for-loop at the bottom then I think that looks like it should work. You'll also have to uncomment the tail prefab field and fill that in.
Let me know if you have issues after that :)
Answer by VanessaMinehan · Nov 11, 2020 at 02:48 AM
Thankyou jackmw94 your solution worked! :D I rearranged the code like you said, at first I thought it didn't work and tried some other things, but in the end what you said worked because I didn't see it working at first because the prefab was so tiny haha! here's the code re-arranged
GameObject leader = gameObject; //start with the head
for (int i = 0; i < numSegments; i++)
{
SerpentSegmentMovement s = Instantiate(segmentPrefab, transform.position, Quaternion.identity);
s.Init(leader);
leader = s.gameObject;
}
SerpentTailMovement t = Instantiate(tailPrefab, transform.position, Quaternion.identity);
t.Init(leader);
leader = t.gameObject;