-1

Hi my problem is in Unity, I am a beginner in c#, my gameObject is not triggering the collider that is set on the plane of the game, in order for it to reset it's position.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasketballSpawnScript : MonoBehaviour
{ 
    public Transform respawnPoint;

    void OnTriggerEnter(Collider other)
    {    
        if (other.gameObject.CompareTag("Basketball"))
        {    
            other.gameObject.transform.position = respawnPoint.position;
        }    
    }
}

This script is attached to the plane and the gameobject is tagged with Basketball, when it enters the collider of the floor it should transform it's position to the original position.

I cannot see what is wrong, can I receive some help?

P.S I get this error when other gameobject go through the collider too.

NullReferenceException: Object reference not set to an instance of an object

Alex Myers
  • 4,257
  • 7
  • 17
  • 33
Zestria
  • 1
  • 3

2 Answers2

0

If using a Transform for spawn point, remember to set the value of it in the inspector menu.

public Transform respawnPoint;

void OnTriggerEnter(Collider other)
{    
    if (other.CompareTag("Basketball"))
        other.transform.position = respawnPoint.position;
}

else

public Vector3 respawnPoint = Vector3.zero;

void OnTriggerEnter(Collider other)
{    
    if (other.CompareTag("Basketball"))
        other.transform.position = respawnPoint;
}
Mohammad Zamanian
  • 694
  • 1
  • 7
  • 16
0
private void OnTriggerEnter(Collider other){

    if(other.gameobject.tag=="Basketball"){

       other.gameobject.transform.position = respawnPoint;
    }
}

I hope it helps you.

dustinos3
  • 888
  • 2
  • 15
  • 25