Hi Guys,
I am trying to update a Text component attached to another script.
//Attached to a Script which detects collisions.
//Find the gameObject named PlayerScript.
GameObject _PlayerScript = GameObject.FindGameObjectWithTag ("PlayerScript");
//Get the PlayerScript Component from the PlayerScript object.
PlayerScript _PlayerFunction = _PlayerScript.GetComponent<PlayerScript> ();
//Update the score in the PlayerScript methods.
_PlayerFunction.updateScore (100);
Inside the PlayerScript.cs which is attached to an empty gameobject also named PlayerScript (Tag) and has 2 Public Text elements which I have dragged the Score text and Remaining Missiles text elements in the inspector.
public void updateScore(int value)
{
Debug.Log ("Score Called");
playerScore.text = " Score : " + value;
}
The problem I am having is that the _PlayerFunction.updateScore() is never called as the Debug.log message never appears, Any ideas please??
Many thanks!
Are you getting any errors? My first guess is you aren’t finding anything. Or if you find the object, it is missing the script?
I would toss some debugs in there to make sure _PlayerScript and _PlayerFunction aren’t null.
Are you perhaps wanting type instead of tag? Unity - Scripting API: Object.FindObjectOfType
If you only have one PlayerScript in your scene, that might be the better choice.
I dont get any errors at all from this code which is why im confused, How would I check to ensure that the _PlayerScript GameObject was found ?? Im really new to Unity so still learning am guessing
If(_PlayerScript == null) { Debug.Log("No Playerscript gameobject");}
Just tried this
void OnCollisionEnter(Collision colInfo)
{
ContactPoint contact = colInfo.contacts[0];
Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal);
Vector3 pos = contact.point;
Vector3 scale = transform.localScale;
int getId = GetComponent<MissileController> ().arrayIdentifier;
// Debug.Log (colInfo.gameObject.tag);
if (colInfo.collider.tag == "Base" || colInfo.collider.tag == "Turret") {
GameObject explosion = Instantiate (explosionEffect, pos, transform.rotation);
ParticleSystem explosionParticles = explosion.GetComponent<ParticleSystem> ();
float durationOfExplosion = explosionParticles.duration + explosionParticles.startLifetime;
Collider[] colliders = Physics.OverlapSphere (pos, radius);
foreach (Collider nearbyObj in colliders) {
// Debug.Log (nearbyObj.GetComponent<Collider>().name);
Rigidbody rb = nearbyObj.GetComponent<Rigidbody> ();
if (rb != null) {
if (nearbyObj.tag != "Missile" && nearbyObj.tag != "Floor") {
GameObject myScriptObject = GameObject.FindGameObjectWithTag("missileSpawner");
MissileScript myScript = myScriptObject.GetComponent<MissileScript> ();
//Find the gameObject named PlayerScript.
GameObject _PlayerScript = GameObject.FindGameObjectWithTag ("PlayerScript");
//Get the PlayerScript Component from the PlayerScript object.
PlayerScript _PlayerFunction = _PlayerScript.GetComponent<PlayerScript> ();
if (_PlayerScript == null) {
Debug.Log ("No Playerscript gameobject");
} else {
Debug.Log ("PlayerScript OK");
}
if (_PlayerFunction == null) {
Debug.Log ("No player functions found");
} else {
Debug.Log ("PlayerFunctions OK");
}
//Update the score in the PlayerScript methods.
_PlayerFunction.updateScore (100);
//This removes a possible target from the MissileScript script List
myScript.removeTarget (getId);
//myScript.showLocations ();
//This works upon collision with a Turret or Base
Destroy (nearbyObj.gameObject);
//Destroys the Projectile
Destroy (gameObject);
Destroy (explosion, durationOfExplosion);
}
}
}
}
}
And i dont even get a Debug.log from these?? Its almost like it skips past without even executing those lines for some reason. The missileScript does execute as a valid target is removed from it,s list so i cant see why this doesnt work.
Thanks!
Dont use “Find” methods. In the scene, just drag the thing you’re trying to find into a public field and use that directly.
I have attacjed an image in the hope that it sheds some light on this.
Would that also work if i declared Public Text in the MissileCollision script and dragged the Score Text object into the Inspector ? I thought I had to keep the Score methods and manipulation within the player script so i can increase and decrease score as necessary.
I think I worked out whats going wrong, I will post the solution if I do manage to work it out.
So sorry, I was having a stupid moment and realised I was calling the methods from a different script as I have a prefab explosion which is instantiate and when the projectile ie… Missile collides with it I am calling the ParticleCollisionDetection Script.
public class ParticleCollisionDetection : MonoBehaviour {
public GameObject _PlayerScript;
public PlayerScript _PlayerFunctions;
// Use this for initialization
void Start () {
_PlayerScript = GameObject.FindGameObjectWithTag ("PlayerScript");
_PlayerFunctions = _PlayerScript.GetComponent<PlayerScript> ();
}
// Update is called once per frame
void Update () {
}
void OnParticleCollision(GameObject other)
{
Rigidbody rb = other.GetComponent<Rigidbody>();
if (rb) {
if (other.tag == "Missile") {
Destroy (other.gameObject);
Debug.Log ("HIT");
_PlayerFunctions.updateScore (100);
}
}
}
Many thanks for all the replies!
Glad you got it figure out 