Reputation: 184
using UnityEngine;
using System.Collections;
public class Egg : MonoBehaviour {
// Use this for initialization
void Start () {
PlayerController playerScript = GetComponent<PlayerController> ();
}
void OnTriggerEnter2D(Collider2D coll)
{
this.GetComponent<SpriteRenderer>().enabled = false;
playerScript.Increment();
}
}
It won't let me use the Increment function in the OnTrigger function.
Upvotes: 0
Views: 39
Reputation: 984
the variable playerScript is declared inside the start
method, there is no variable called playerScript in the OnTriggerEnter2D
method.
you should do something like this instead:
public class Egg : MonoBehaviour {
private PlayerController playerScript;
// Use this for initialization
void Start () {
playerScript = GetComponent<PlayerController> ();
}
void OnTriggerEnter2D(Collider2D coll)
{
this.GetComponent<SpriteRenderer>().enabled = false;
playerScript.Increment();
}
}
Upvotes: 2
Reputation: 2390
Make sure OnTriggerEnter2D
is really triggered, simply add Debug.Log("Boom!");
inside OnTriggerEnter2D
.
Secondly, check if playerScript
is found. Add this inside OnTriggerEnter2D body
Debug.Log("Is playerScript null? "+ (playerScript == null));
Upvotes: 0