Hi,
this code should look if player is closes than 10 meters to this object and then give option to pick it up. But it does not do that. Unity gives error :
NullReferenceException: Object reference not set to an instance of an object
ülevõttmine.Update () (at Assets/ülevõttmine.cs:20)
How do I fix that? This information helps me a lot, I have been stuck on this for a long time.
public class ülevõttmine : MonoBehaviour
{
int kaugus = 10; // Maximum distance that player can be away from pickupable object.
public GameObject ülesvõttja; // player that picks up the object.
public GameObject relvavahetamine; //After pickup this becomes parent of pickupable object.
public GameObject koht; // place where pickupable object is placed after the pickup.
public void ülesvõttmine()
{
this.transform.position = koht.transform.position + transform.up;
this.transform.SetParent(relvavahetamine.transform, true);
}
void Update()
{
if (Vector3.Distance(this.transform.position, ülesvõttja.transform.position) < kaugus)
{
GetComponent().aktiveerimine(); //shows text to the player that object can be picked up.
if (Input.GetButton(“f”)==true)
{ ülesvõttmine(); }
}
}
}