Search Unity

Teleporting C# Script Problems

Discussion in 'Scripting' started by taschtasch, May 30, 2015.

  1. taschtasch

    taschtasch

    Joined:
    May 27, 2015
    Posts:
    9
    Hello!
    I'm working on an easy one-way teleporting script in C#.
    I'm pretty much a beginner in Coding, thats why I have no idea what to do, but here's my script so far:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Teleportieren : MonoBehaviour
    6. {
    7.     public Transform ziel;
    8.     public GameObject player;
    9.  
    10.     void Start ()
    11.     {
    12.         ziel = GetComponent<Zielkoordinate>();
    13.         player = GetComponent<Spieler>();
    14.       }
    15.    
    16.  
    17.     void OnTriggerEnter(Collider col)
    18.     {
    19.         if(col.tag == "Player")
    20.         {
    21.             player.transform.position = ziel.transform.position;
    22.  
    23.  
    24.         }}}
    25.  
    26.  
    There's something wrong with the Spieler and Zielkoordinate! (That's what the errors say) And I think it's because they are gameobjects and you can't use GetComponent in that way?!
    Zielkoordinate is an empty gameobject that just stores the koordinations of the target! (I have to do it that way)
    Oh and ziel is the target of the teleporter.
    I'd be really really happy if someone could help me!
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Is this script on the ziel &/or player objects? You're using GetComponent, which simply looks for the component you specify on the current game object that this script is being executed on. If the game object that the script is on does not have those specific components, you'll receive errors. It sounds like you most likely need to locate these other game objects and then access the individual components you need from them.
     
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    check out my signature for a tutorial on GetComponent.
     
    krougeau likes this.