Reputation: 19
my code in GameController is :
public void AddScore(int newscore)
{
score += newscore;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "score " + score;
}
and my code in destroyByContact :
public GameController gameController;
void OnTriggerEnter(Collider other)
{
if (other.tag =="boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "player")
{
Instantiate(playerexplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
and unity display this error :
error CS1061: Type GameController' does not contain a definition for
AddScore' and no extension method AddScore' of type
GameController' could be found (are you missing a using directive or an assembly reference?)
Upvotes: 1
Views: 409
Reputation: 721
I don't know about your actual destroyByContact class. But I think you might be not declaring the object or referencing it.
using UnityEngine;
using System.Collections;
public class destroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("boundary"))
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
Please note that code in
start()
tries to get the reference of the GameController script & If the script is not referred it will print the following log.
Upvotes: 1