Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Transform.position to an emty game object

Discussion in 'Scripting' started by nuketheduke, Apr 13, 2022.

  1. nuketheduke

    nuketheduke

    Joined:
    Feb 11, 2022
    Posts:
    4
    Hi all.
    Im pretty new to unity, and i am working on a 2d platformer.

    I am using portals to transform.position player to another position. I started up with hardcoding the new position in the script, but then i will have to a new script everytime i use a portal.
    I need help to find a way to set the new location i the inspector (emty gameobject).

    The script is attached to the portal.

    This is what i use now
    Code (csharp):
    1.  
    2. public class Transit : MonoBehaviour
    3. {
    4.     private GameObject player;
    5.  
    6.     private void Start()
    7.     {
    8.         player = GameObject.Find("Player");
    9.     }
    10.  
    11.     void OnTriggerEnter2D(Collider2D collision)
    12.     {
    13.         if (collision.tag == ("Player"))
    14.         {
    15.             player.transform.position = new Vector3(-15.98f, 2.11f, 0);
    16.         }
    17.     }
    18. }
    19.  
     
    Last edited: Apr 13, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,969
    Welcome!!

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.

    The magic sauce you need is to obtain a reference to the Transform portal where you want the player to go, and to copy that Transform's
    position
    to your player's position.

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
    Bunny83 likes this.
  3. nuketheduke

    nuketheduke

    Joined:
    Feb 11, 2022
    Posts:
    4
    Edited, and thanks.

    Im not accessing Other scenes or scripts.
    When player hits the portal, he is relocated, but in the same scene
     
    Last edited: Apr 13, 2022
  4. nuketheduke

    nuketheduke

    Joined:
    Feb 11, 2022
    Posts:
    4
    I found a way. Turns out is was pretty simple

    Code (csharp):
    1.  
    2. public class Transit : MonoBehaviour
    3. {
    4.     [SerializeField] private Transform Player;
    5.     [SerializeField] private Transform Transitpoint;
    6.  
    7.        void OnTriggerEnter2D(Collider2D Other)
    8.     {
    9.         Player.transform.position = Transitpoint.transform.position;
    10.     }
    11.    
    12. }
    13.  
    It works for me :)
     
    Kurt-Dekker and Bunny83 like this.