Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nazdorovia · Dec 11, 2017 at 02:12 AM · instantiatelistdestroyontriggerenter

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!

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

1 Reply

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

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 );
    }
}

Comment
Add comment · Show 2 · 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 nazdorovia · Dec 11, 2017 at 05:51 PM 0
Share

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.

avatar image fdz_ nazdorovia · Dec 11, 2017 at 07:26 PM 0
Share

$$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.

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

96 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

Related Questions

walk through an object once 0 Answers

Destroying a GameObject w/ children isn't properly destroying everything? 1 Answer

A node in a childnode? 1 Answer

C# override a destroyed GameObject in a List 1 Answer

Instantiate spawns 2 instead of 1, help? 2 Answers


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