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

.position not working

Discussion in 'Scripting' started by Sugarmaster100, Jan 5, 2019.

  1. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
    i am trying to spawn a object above anouther but .position is not working
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Collider : MonoBehaviour
    7.  
    8. {
    9.     public GameObject Colliderforcoin;
    10.     public GameObject Colliderspawn;
    11.  
    12.     private void OnTriggerEnter2D(Collider2D other)
    13.  
    14.     {
    15.  
    16.         Debug.Log(other.name + " The object has gone through");
    17.  
    18.     }
    19.  
    20.     private void OnTriggerExit2D(Collider2D collision)
    21.  
    22.     {
    23.  
    24.         Colliderforcoin.GetComponent<Collider2D>().isTrigger = false;
    25.  
    26.         Vector3 pos = new Vector3(0f, 10f, 0f);
    27.         Instantiate(Colliderspawn, Colliderforcoin.position + pos, Colliderforcoin.rotation);
    28.     }
    29. }
    30.  
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    Does the object spawns at all?
    Put some Debug.Log () in OnTriggerExit2D, to see what is happening.
     
  3. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
    I Cant even start the game
     
  4. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    Ah right.
    GameObject does not have position.
    But its component Transform has.
    So you need instead
    Colliderforcoin.position
    do
    Colliderforcoin.transform.position