Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change Room Trouble. Please Help.

Discussion in '2D' started by Lord-Kiyo, Sep 14, 2018.

  1. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    Hello. I am having a strange anomaly that I just can't figure out. I have a load scene function which my character and camera have a DontDestroyOnLoad(transform.gameObject);
    When I put the load trigger close to the start point, it works fine. However when I put the load trigger where I want it, and I go touch it, it deletes the character. I don't know why it works one way but not the other. The only thing I think is changing is distance.

    Here are some of the codes that I am using. I hope I can get some sort of help and a working solution.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class KadinControle : MonoBehaviour

    public string startPoint;


    // Use this for initialization
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    Cc = GetComponent<CapsuleCollider2D>();
    slideMove = false;

    DontDestroyOnLoad(transform.gameObject);
    }



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class LoadNewArea : MonoBehaviour {

    public string levelToLoad;
    public string exitPoint;
    private KadinControle theKadin;

    // Use this for initialization
    void Start ()
    {
    theKadin = FindObjectOfType<KadinControle>();
    }

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

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.name == "Kadin")
    {
    GameController.instance.curentRoom = levelToLoad;
    SceneManager.LoadScene(levelToLoad);
    theKadin.startPoint = exitPoint;
    }
    }

    }



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class LoadStartPoint : MonoBehaviour {

    private KadinControle theKadin;
    private GameManager theManager;
    private CamraControle theCamra;

    public string pointName;


    // Use this for initialization
    void Start ()
    {
    theKadin = FindObjectOfType<KadinControle>();

    if (theKadin.startPoint == pointName)
    {
    theKadin.transform.position = new Vector3(transform.position.x, transform.position.y, 0);

    theCamra = FindObjectOfType<CamraControle>();
    theCamra.transform.position = new Vector3(transform.position.x, transform.position.y, theCamra.transform.position.z);

    theManager = FindObjectOfType<GameManager>();
    theManager.transform.position = transform.position;
    }

    }

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

    }
    }
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I think just
    Code (CSharp):
    1. DontDestroyOnLoad(gameObject);
    2.  
    Unless you're specifically trying to preserve the players location, the transform. However, it looks like it's destroying your players game object and storing his location.

    Code (CSharp):
    1. [SerializeField]
    2. private GameObject player; // Stores the player game object
    3.  
    4. void Start(){
    5.  
    6. player = GetComponent<GameObject>();
    7. DontDestroyOnLoad(player);
    8.  
    9. // I don't know if you can use player.transform.position with
    10. //this because that's about the same as
    11. //transform.gameObject? So try just the player first and see if
    12. // that helps.
    13. }
    14.  
    15.  
     
  3. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    A little off-topic.

    Is-> player = GetComponent<GameObject>();
    the same as -> player = this;
    ???
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I'm not sure on that. I thought "this" was kinda a loop that could store constructors or game objects and load them dynamically. So I suppose it's possible, but I wouldn't write my code like that. (Personally)
     
  5. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    this
    returns and instance of the class you are working from. So if you used that keyword in the context of a component, it would return the instance of that component. Using
    GetComponent<GameObject>()
    is a pretty odd thing to do since
    GameObject
    is not a component. You can get a reference to a
    MonoBehaviour
    's gameObject via
    this.gameObject
    or just
    gameObject
    .

    transfrom.gameObject
    and
    gameObject
    return the same exact thing, the former just skips referencing the transform.

    I see you are calling
    DontDestroyOnLoad
    on your player. It's odd that its getting destroyed. Is your player object a child of another object perhaps?

    When managing scenes, I like to additively add a seperate scene for objects I want to stick around between scene changes. Like the player and camera.
     
  6. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    I gave this a try. But it did not work. It actually deleted the character much quicker. The idea is that the Load New Area script tells what scene to load, and gives the player a spawn point to go to in that scene. It works fine the first load new scene I do. but don't work on the second. But if I bring the load new area point closer to the spawn point, then it works. I have no idea why it does such a strange thing.
     
  7. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    I tried replacing this, with gameObject, and same thing happens. The first load works fine, but on the second load it just deletes the character. But it don't delete the other DontDestroyOnLoad objects.
    I used these tutorials to get the moving to new areas

     
  8. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Like I was saying before, it does matter which route you take to get a reference to the gameObject.
    gameObject
    transfrom.gameObject
    and
    this.gameObject
    all point to the same exact object.

    I could see how starting your character inside a trigger might not work since your
    DontDestroyOnLoad
    is called from a start function. I'd move it to Awake and possibly give it the
    DefaultExecutionOrder
    attribute with a negative value to make sure it's getting called before other Awake functions get called.

    The only things I can think of that would be an issue is that for some reason
    DontDestroyOnLoad
    is not getting called. Or maybe the gameObject that set to not destroy is the child of another object. Although I don't know how Unity handles that edge case.

    If you are still have trouble, you can send me a bare bones version of your project and I can try to debug it for you.
     
  9. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    I tried to put in the Awake function in there, but the program did not like it. So I don't know how to do that properly. I am new to Unity, so Only know some basic things. How would I be able to send you a project? I will see if I can make a bare bones version.
     
  10. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Sorry, missed your reply. You would zip up the Assets, project settings, and packages folders and then click the 'upload a file' button here on the forum.
     
  11. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    I just saved a new copy and started to strip away several entities to make it bare bones as you suggested. Then I tested it one last time, and now for some odd reason its working properly.

    The only thing I can think of right now is that maybe their is a strain on memory? Would not think there would be one because I am doing a 2D game. I am using tiles Perhaps I need to make them smaller?

    This site tells me that I can't upload the file because its too big. Perhaps I need to shrink all my sprites? But that could mess up all my animations, would have to redo alot of thing.
     
  12. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Interesting that it works after stripping it down. You could try sharing via Google drive and creating a sharable link.
     
  13. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    I was able to upload it to my Diveanart account. https://sta.sh/021k5uhknf4g
    Not sure how useful it will be now, if it decided to start working now after I gutted everything. But perhaps you can figuer out what I need to do to keep the regular stuff and get it working?
     
  14. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Hmmm, I can't reproduce it in the gutted version. Make sure Kadin ends up in the DontDestroyOnLoad in the scene hierarchy.
     

    Attached Files:

  15. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    He always starts in the DontDestroyOnLoad hierarchy. but then he still gets destroyed in the next load. Perhaps I could try reducing the size of the sprites of everything, and see if that helps. or turn things off to see what is making it do it.
     
  16. Lord-Kiyo

    Lord-Kiyo

    Joined:
    Jun 30, 2018
    Posts:
    20
    I went through the long processed of making the sprites smaller. And I still have the same problem. I will try and give you the exact file I have, so you can see it destroys the character. https://sta.sh/0h5n4p9qpld