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

Resolved Teleport script is being...weird.

Discussion in 'Scripting' started by QuickiSticki, Mar 18, 2021.

  1. QuickiSticki

    QuickiSticki

    Joined:
    Nov 18, 2020
    Posts:
    26
    My teleport script is being funky. It does not throw any errors. When in game, it only works part of the time. I can shoot the same spot over and over and it will work sometimes and then will not work.

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

    public class TeleportLand : MonoBehaviour
    {
    private float lifeTimer = 2f;
    private float timer;
    void Start()
    {
    GameObject player = GameObject.Find("Player Character");
    player.transform.position = this.transform.position;
    GetComponent<AudioSource>().Play();
    }
    void Update()
    {
    timer += Time.deltaTime;
    if (timer >= lifeTimer)
    {
    Destroy(gameObject);
    }
    }
    }

    Checking the code with debug, everything checks out. I am thinking that the player.transform.position change is the problem. Perhaps I am not wording it correctly or maybe the code is slow. Either way, it is not reliable. The only other thing I can think to mention is that when it does NOT work, my character does shake a bit in place, but doesn't move.

    And yes, I know, GameObject.Find is not great to use.

    Thank you guys for any ideas or help you can give!
     
    Last edited: Mar 18, 2021
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Start() method works ONE TIME no more.
     
  3. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    and maybe you have to reset the timer to zero after the destroy statment
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    The script seems ok, maybe look into when it's being instantiated as being the problem?
     
  5. QuickiSticki

    QuickiSticki

    Joined:
    Nov 18, 2020
    Posts:
    26
    Thank you guys for the replies. I seem to have fixed it. I only want the position change to occur once, so Start() was fine. However, I moved the position change to Update and then made a bool so it would still only run once, and it seems to work correctly now. Here is the new code, if you are interested:

    public class TeleportLand : MonoBehaviour
    {
    private float lifeTimer = 2f;
    private float timer;
    private GameObject player;
    bool positionChanged = false;
    void Start()
    {
    player = GameObject.Find("Player Character");
    GetComponent<AudioSource>().Play();
    }
    void Update()
    {
    if(!positionChanged)
    {
    player.transform.position = this.transform.position;
    positionChanged = true;
    }

    timer += Time.deltaTime;
    if (timer >= lifeTimer)
    {
    Destroy(gameObject);
    }
    }
    }
     
  6. QuickiSticki

    QuickiSticki

    Joined:
    Nov 18, 2020
    Posts:
    26
    This object gets destroyed and remade each time you shoot the teleport, so no need to reset the timer, and it really only needs to be called once. :)
     
  7. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Hapy it works, but AcidArrow is probably right. take a look to where you generate the new gameobject