0
\$\begingroup\$

In my game when you lose, A panel with a button on it appears and if you don't have enough coins, You may be directed to shop by hitting shop button. when buying is completed you can return to the scene from where you left it and resume the game. I'm using PlayerPrefs to save properties like slider.value my player position timescale etc, But I can't access to Transform of an Instantiated GameObject (Clone). it acts like obstacle and is Instantiated continuously. Here is my script;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using CodeStage.AntiCheat.ObscuredTypes;
using CodeStage.AntiCheat.Detectors;

public class Spawner5 : MonoBehaviour {

    public float maxTime=1;
    public float Timer = 0;
    public GameObject pipe;
    public float height;



    // Use this for initialization
    void Start () {

        pipe = GameObject.FindGameObjectWithTag ("Gear");
    }

    // Update is called once per frame
    void Update () {

        if (Timer > maxTime) {

            GameObject newpipe = Instantiate (pipe);


            newpipe.transform.position = transform.position + new Vector3(4, Random.Range (-height, height) ,0);

            Destroy (newpipe, 15);
            Timer = 0;

        }

        Timer += Time.deltaTime;
    }
} 
\$\endgroup\$
7
  • \$\begingroup\$ What exactly do you mean with "can't access to Transform"? Does the code you posted here generate some kind of error?`And what exactly do you mean "it acts like obstacle and is Instantiated continuously." - how is that different from the expected behavior? And how are PlayerPrefs and some "slider" related to this problem? \$\endgroup\$
    – Philipp
    Jul 17, 2020 at 7:37
  • \$\begingroup\$ I want to save Transform of a clone (newpipe). my game is like flappy bird and this Instantiated Object acts as an obstacle. Slider value is for progress in game. As I said I don't have any problem saving slider value or player position but Transform of the instantiated object. \$\endgroup\$
    – amin007
    Jul 17, 2020 at 8:02
  • \$\begingroup\$ I still don't understand. The code you posted does use transforms, but there is nothing in there which saves or loads transforms with PlayerPrefs. \$\endgroup\$
    – Philipp
    Jul 17, 2020 at 8:11
  • \$\begingroup\$ @amin007 do you want to save position of your newly created obstacle? So that when player is returned, obstacle could be reinstantiated? \$\endgroup\$
    – eLTomis
    Jul 17, 2020 at 9:27
  • 1
    \$\begingroup\$ I think OP is saying they want to save the locations of all pipes, so that after transitioning to the game over scene and resuming, they can come back to this scene in exactly the state they left it. So it's not about "accessing" the transform, but about storing its position. However, a better solution is to simply not leave this scene at all. Pause your movement/spawning, show the game over screen on a UI panel in front of this content, and if the player chooses to resume, hide the game over UI again to reveal the scene is still in memory, exactly as you left it, no saving/loading required. \$\endgroup\$
    – DMGregory
    Jul 17, 2020 at 11:57

1 Answer 1

-1
\$\begingroup\$

I solved my problem by using LoadSceneMode.Additive

\$\endgroup\$
1
  • \$\begingroup\$ This answer would be better if it explained in detail how you solved your problem with LoadSceneMode.Additive \$\endgroup\$
    – DMGregory
    Aug 6, 2020 at 9:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .