Forum rules - please read before posting.

Spawning Multipile instances of Prefab and Remembering their Transform

edited September 2024 in Technical Q&A

I suppose it can have very simple solution but I could not find .
I have prefab. I spawn two instance of it inb the scene.
They have hotspot and can be picked into inventory and ofcourse would be removed from the scene.
But when i use Remember transform Only one apears to be in its place and the other one is forgotten.

I was trying to uncheck |Remember Transform but it seems impossible so i thought my logic and understanding is wrong.
sorry for too much of questions.

Comments

  • I must add that i do not add the Prefab by using AC add in action list . since i wanted to spawn my prefab object using an animation event, i took advantage of a simple script and i can assume my code can not sync with AC Constant IDs as it should! this is the code i use:
    `using UnityEngine;

    public class BallDropController : MonoBehavi0ur
    {
    public GameObject ballPrefab; // prefab to instantiate
    public Transform dropPoint1; // The first drop point
    public Transform dropPoint2; // The second drop point

    private int ballsDropped = 0; // Tracking which ball to drop
    
    // This method for Animation Event
    public void DropBall()
    {
        // Drop the first ball
        if (ballsDropped == o)
        {
            Instantiate(ballPrefab, dropPoint1.position, Quaternion.identity);
            ballsDropped++;
        }
        // Drop the second ball
        else if (ballsDropped == 1)
        {
            Instantiate(ballPrefab, dropPoint2.position, Quaternion.identity);
            ballsDropped++;
        }
    }
    
    //  Method to reset ball dropping
    public void ResetBallDrop()
    {
        ballsDropped = 0; // Reset to allow dropping balls again
    }
    

    }
    `

  • If you're instantiating an object through script, call its RememberTransform's OnSpawn function afterwards.

  • edited September 2024

    Thanks Here is what i came up with.
    first of all
    I did not spawned prefab through AC workflow since i could not find a way to trigger them being spawned by animation event( I also added a cutscene component and used enimation event Run action or Process Action but could not get the resul.I wanted to spawn the balls at a specific frame of an animated hose.

    Inspector description:
    On the Ball prefab:
    Constant ID Retain Prefab is unchecked.
    Remember Hotspot(since each ball has a Hotspot Child in prefab which works with Set Interaction Parameters: "Reference scene instance" forRemo0ve.
    Remember Transform: Global ,
    Save scene presence: checked
    Retain in Prefab is Locked/checked.
    inside scene and to have method for animation event i have this
    `using UnityEngine;
    using AC;

    public class BallDropController : MonoBehaviour
    {
    public GameObject ballPrefab; // The ball prefab to instantiate
    public Transform dropPoint1; // The first drop point for the ball
    public Transform dropPoint2; // The second drop point for the ball

    private int ballsDropped = 0; // To track which ball to drop
    
    // This method will be called by the Animation Event
    public void DropBall()
    {
        GameObject newBall = null;
    
        // Drop the first ball
        if (ballsDropped == 0)
        {
            newBall = Instantiate(ballPrefab, dropPoint1.position, Quaternion.identity);
            ballsDropped++;
        }
        // Drop the second ball
        else if (ballsDropped == 1)
        {
            newBall = Instantiate(ballPrefab, dropPoint2.position, Quaternion.identity);
            ballsDropped++;
        }
    
        // Ensure the ball is properly set up for persistence
        if (newBall != null)
        {
            RememberTransform rememberTransform = newBall.GetComponent<RememberTransform>();
            if (rememberTransform != null)
            {
                // Call OnSpawn to register the object with Adventure Creator
                rememberTransform.OnSpawn();
    
                // Assign a unique Constant ID to the new instance
                ConstantID constantID = newBall.GetComponent<ConstantID>();
                if (constantID == null)
                {
                    constantID = newBall.AddComponent<ConstantID>();
                }
                constantID.AssignInitialValue();
            }
        }
    }
    
    // Call this method if you need to reset ball dropping
    public void ResetBallDrop()
    {
        ballsDropped = 0; // Reset to allow dropping balls again
    }
    

    }
    `

    as you see to register the object with Adventure Creator i used rememberTransform.OnSpawn(); and hopefuly assigned a new ID to newly spawned. but then i can not unlock the Remember transform in the Inspector. so it seems only one transform is rememberd the one with the original prefab or the first constant id which probably matches with it?!!!

    so still i get only one of the balls in the scene if i switch scene before i pick them.
    I can also get around just cheat in a smipler way but i thought maybe i can just do it in a more clean way.

  • RememberTransform derives from ConstantID - don't add/modify it beyond that.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.