Forum rules - please read before posting.

"Remember Transform" Spawning to Wrong Position

edited February 2022 in Technical Q&A

Hello,
Amazing asset, with all its great content and features, it's the one that's included in every project I start. I think I'm an intermediate at coding and Unity and have a bit of a problem with the save system and remembering transforms. I'm making a model railroading game where the player can choose from different track sections to make any track plan they can imagine. The player chooses a track section from a menu and right clicks to instantiate it at the mouse position. This new track section has a sphere collider on each end; the player can click on either one of these and then on the end of an already placed section and the new section snaps into place onto the old. When loading a saved game however, the track sections are spawned at the original mouse position transforms, not the new ones that they snapped too. There are no error messages and all child transforms inside the prefabs are at zero. The component is set at:

Co-ordinate Space = Global
Save change in parent = Check
Save scene presence = Check
Linked ID = Same number as in the "Constant ID number" component

Here's the instantiation code:

         trackPrefab_MedStraightHolder = Instantiate(trackPrefab_MedStraight, hitInfo.point, Quaternion.identity);      
         trackPrefab_MedStraightPlaced.Add(trackPrefab_MedStraightHolder);        
         trackPrefab_MedStraightHolder.GetComponent<AC.RememberTransform>().OnSpawn();

And the code that connects the track pieces:

   private GameObject GetPoint_1()
    {

        // Get connection point on just instantiated track piece - 'Selected Point'
        point_1 = hit1.transform.gameObject; 

        point_1.name = "Sphere #" + counter;
        point_1.transform.parent.name = "Track #" + counter;

        Debug.Log(point_1);

        return point_1;

    }

    private GameObject GetPoint_2()
    {   
    // Get connection point on already placed track section - 'Destination Point'
        point_2 = hit2.transform.gameObject; 

        Debug.Log(point_2);

        return point_2;

    }

    private void ConnectSections()
    {
        if (point_1 != null && point_2 != null)
        {
            //will be parenting to this object (the selected point)
            point_1.transform.SetAsLastSibling();

            // parent unselected point on new track piece to the selected one
            point_1.transform.parent.transform.GetChild(0).transform.SetParent(point_1.transform);

            // parent gameobject containing scripts and meshes to selected point
            point_1.transform.parent.transform.GetChild(0).transform.SetParent(point_1.transform); 

            if (point_1.transform.parent.transform.GetChild(0).gameObject != null)
            {
                // if track piece has a third point (eg a turnout), parent it to selected point
                point_1.transform.parent.transform.GetChild(0).transform.SetParent(point_1.transform);
            }

            // Connect the selected point to the destination point. This is the position "Remember Transform" should be      
                 saving...

            point_1.transform.position = point_2.transform.position; 

        }

Thank you for your attention...Much appreciated.

Comments

  • What's your AC version, and does this happen when only one such object is in the scene?

    It looks like your parenting tracks to one another, which may cause position issues depending on the order in which tracks are loaded.

    Try keeping all tracks as "root" objects in the Hierarchy.

    If you click the Manage save-game files button inside the Settings Manager, you'll bring up the Save-game file Manager, which details all data stored in a save-file. See if you can find the data block for the object to check that it's saved position is correct - it'll be in the "Remember data" section. There'll be a lot to wade through, so keep the number of such objects down to only one or two while testing.

  • Thank you for the quick response time! Yes, the problem was with my weird way of parenting stuff. This new code now seems to work fine.

    Many thanks for the assistance!

    private void ConnectSections()
    {

        // Connect the selected point to the destination point
    
        if (isSphere_A)
        {
            point_1.transform.parent.transform.position = new    
            Vector3(point_2.transform.position.x, point_2.transform.position.y, 
            point_2.transform.position.z - 9.4f);
        }
        else
        {
            point_1.transform.parent.transform.position = new 
            Vector3(point_2.transform.position.x, point_2.transform.position.y, 
            point_2.transform.position.z + 9.4f);
        }
    
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.