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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using transform.position on a gameobject that the script isn't attachted to

Discussion in 'Scripting' started by spartaboy300, Aug 12, 2015.

  1. spartaboy300

    spartaboy300

    Joined:
    Jun 24, 2014
    Posts:
    16
    I have a script attached to a trigger collision at a shop door, and I was using it to change scenes, but I'm now wanting to just use transform.position to teleport the player in the same scene to the shop. But what I'm wondering is how to use the transform command on the player while the script is a component of the trigger collision. Any help is appreciated! :) (I use C# btw)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    All you need is a reference to the object you want, and you can get that in a number of different ways depending on the situation. In your case, since you're using it in a trigger collision, you can use the "other" collider that gets passed into the function:
    Code (csharp):
    1. void OnTriggerEnter(Collider other) {
    2. other.transform.position = somePosition;
    3. }
     
  3. spartaboy300

    spartaboy300

    Joined:
    Jun 24, 2014
    Posts:
    16
    Awesome that's exactly what I'm looking for, thank you!