Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by VanessaMinehan · Nov 10, 2020 at 08:38 AM · instantiateprefabsnakechainfollowing

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

etetet.jpg (193.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VanessaMinehan · Nov 11, 2020 at 02:49 AM 0
Share

thankyou jack <3

avatar image
0

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jackmw94 · Nov 11, 2020 at 02:51 AM 0
Share

Nice! :D Cool project too

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems instantiating a prefab (javascript) 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

How to instantly move a newly instantiated prefab? 2 Answers

How do I stop infinite instantiation? 1 Answer

Instantiating prefabs as child of a gameobject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges