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

Newbie needs help with C#

Discussion in 'Scripting' started by Swiftbanana, Apr 6, 2015.

  1. Swiftbanana

    Swiftbanana

    Joined:
    Apr 6, 2015
    Posts:
    8
    Hello!

    I am new to using Unity and C#.
    Right now I have some issues creating a quite simple code.
    The code is a Death code, and I want collision with a certain object to reset the position of the player.

    Code (csharp):
    1.  
    2. [code=CSharp]using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Death : MonoBehaviour
    6. {  
    7.     public GameObject player;
    8.     private class Vector3
    9.    
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.         if(other.gameObject.tag == "Sea");
    13.         Transform.position = new Vector3(0,5,0);
    14.         print(Transform.position.x);
    15.     }
    16. }
    [/CODE]

    I am aware there are multiple errors here and that I might be using some of the code wrong,
    but if I could get some answers that would be great as I am trying to learn.

    Thank you
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You need to remove the ";" after the if statement and add some {}. Also line 8, doesn't do anything sane. Transform with a captial T is the class, not the instance. So you want to use transform instead.

    I think this is what you want:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Death : MonoBehaviour
    4. {
    5.     public GameObject player;
    6.  
    7.     void OnTriggerEnter(Collider other)
    8.     {
    9.         if(other.gameObject.tag == "Sea")
    10.         {
    11.           transform.position = new Vector3(0,5,0);
    12.           print(transform.position.x);
    13.         }
    14.     }
    15. }
     
  3. Swiftbanana

    Swiftbanana

    Joined:
    Apr 6, 2015
    Posts:
    8
    I managed to solve it with a whole other code and it is working perfectly.
    But thank you so much for the reply PGJ, I really appriciate you were willing to help :)

    /cheers