Search Unity

Script

Discussion in 'Scripting' started by MrNormal, Apr 15, 2021.

  1. MrNormal

    MrNormal

    Joined:
    Apr 9, 2021
    Posts:
    3
    I need some assistance with my code here. I'm wanting to make the player teleport forward on a keypress, but I'm a noob. I'm trying to get the position of the player but I keep getting the error: "'TeleportCtrl' does not contain a definition for 'player.'" What can I change with my script to be able to perform this action? (Aside from changing the values at the bottom, that is just testing a location to teleport to.)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class TeleportCtrl : MonoBehaviour
    5. {
    6.  
    7.     [SerializeField]
    8.     private InputActionReference teleportControl;
    9.  
    10.  
    11.     private void OnEnable() {
    12.     teleportControl.action.Enable();
    13.     }
    14.  
    15.     private void OnDisable() {
    16.     teleportControl.action.Disable();
    17.     }
    18.  
    19.     void Start () {
    20.         if (this.player == null)
    21.             this.player = GameObject.FindGameObjectWithTag("Player");
    22.     }
    23.     void Update () {
    24.         if(teleportControl.action.triggered){
    25.             player.transform.position = new Vector3(20.5f,20.5f,20.5f); // This is where you would like your player to teleport. the best way I have found to find the coordinates of a spot is to place another player model there. I'm sure there is another way, but it worked for me.
    26.             }
    27.         }
    28.     }
    29.  
    30.  
     
    Last edited: Apr 29, 2021
  2. Well, you're missing a
    private GameObject player;
    line around the 9th line of this file for starters.
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    The whole point of this forum to provide a resource for folks who encounter similar issues. Do not delete posts using the edit feature when your question is answered. It is rude and not supportive of the whole reason this forum exists. Your post has been restored.
     
  4. MrNormal

    MrNormal

    Joined:
    Apr 9, 2021
    Posts:
    3
    I apologize. It was a simple fix, so I assumed because I'm a noob it would be easy to other people who actually knew a bit of scripting. I didn't see a 'delete post' option then I got rid of it it so I didn't waste anybody's time. That's why I removed the tags and stuff. As you can tell, this is my first post. I guess I wasn't considering those who don't know much of coding like me. When I get back to my computer, I'll post the upgraded script as an apology. ;) Sorry for the inconvenience.
     
    mopthrow likes this.
  5. MrNormal

    MrNormal

    Joined:
    Apr 9, 2021
    Posts:
    3
    Sorry I've taken a while. I've tried and tried and I can't seem to perfect my script like I want it to work. I eventually found a video for what I wanted and I've linked it below. It's exactly what I was looking for and I'm using that now. Thank you though, and sorry again for withdrawing from others who need the same help.

    For those having trouble, here is a great forward teleportation script:


    Show this guy some love. He's got a small channel but he helped me a lot.
     
  6. JTAGames

    JTAGames

    Joined:
    Mar 7, 2019
    Posts:
    13


    This tutorial might help as well.