2

I want to change the playerCurScore integer (in ScoreManager script) from HarmEnemiesScript.

(Script attached to Object in Scene)

using UnityEngine;
using System.Collections;

public class ScoreManager : MonoBehaviour 
{
    public int playerScore;
    public int playerCurScore;

    public void Start()
    {
        playerScore = 0;
        playerCurScore = 0;
    }

    public void Update()
    {
        playerCurScore = playerScore;
    }
}

(Script attached on Enemy)

using UnityEngine;
using System.Collections;

public class HarmEnemies : MonoBehaviour 
{
    public float enemyHealth;
    public float enemyCurHealth;
    public float playerDamage;

    public void Start()
    {
        enemyCurHealth = enemyHealth;
    }

    public void OnTriggerEnter(Collider theCollision)
    {
        if(theCollision.tag == "Fireball")
        {
            enemyCurHealth = enemyCurHealth - playerDamage;
            Destroy (theCollision);
        }
        if(enemyCurHealth <= 0)
        {
            Destroy (this.gameObject);
        }
    }
}

So how can I change the playerCurScore Int from HarmEnemies. I know that I have to use GetComponent and I can use playerCurScore++;

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Burge: Can you please be more specific on the question? Can't see playerCurScore anywhere in the code. – Anurag Jan 14 '15 at 09:19
  • @Anurag The HarmEnemy Script is attached to the Enemy. The Enemy will be destroyed after Health <= 0. So I think I have to add a script (Which will be the ScorePoints) to add the score in this script, before the enemy gets destroyed. BTW I have added the script now :) Im sorry for this. –  Jan 14 '15 at 16:41
  • 1
    There are several ways to get a reference to the ScoreManager class and then change the value. [see here](http://stackoverflow.com/questions/26551575/unity-how-to-access-a-variable-from-another-script-in-another-gameobject-throug) – Catwood Jan 14 '15 at 16:50
  • The ScorePoint Script is attached to a EmptyGameObject –  Jan 14 '15 at 16:51

2 Answers2

0

You are making several mistakes. The main one is that the ScoreManager script should not be attached to the EnemyGameObject.

Make a separate GameObject and name it ScoreManager. Then find it in the scene and update the score.

 ScoreManager s = GameObject.Find("ScoreManager");
 s.SendMessage("incrementScore", amount);

In the ScoreManager class you need this:

 private static int score;

 public void incrementScore(int amount)
 {
     score += amount;
 }

If you are clever you make the whole ScoreManager class static and then it wouldn't even have to be a MonoBehaviour.

You would then call it like this:

 ScoreManager.incrementScore(amount);

Information about static classes.

apxcode
  • 7,696
  • 7
  • 30
  • 41
0

I could fix it now, thank you for the static hint. my code looks like this now:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour 
{
    public static float playerScore;
    public Text scoreCount;

    public void Update()
    {
        ScoreController ();
    }

    public void ScoreController()
    {
        scoreCount.text = "Score: " + playerScore;
        Debug.Log ("Score: " + playerScore);
    }
}




using UnityEngine;
using System.Collections;

public class HarmEnemies : MonoBehaviour 
{
    public float enemyHealth;
    public float enemyCurHealth;
    public float playerDamage;

    public void Start()
    {
        enemyCurHealth = enemyHealth;
    }

    public void OnTriggerEnter(Collider theCollision)
    {
        if(theCollision.tag == "Fireball")
        {
            enemyCurHealth = enemyCurHealth - playerDamage;
            Destroy (theCollision);
        }

        if(enemyCurHealth <= 0)
        {
            ScoreManager.playerScore = (ScoreManager.playerScore + 1);
            Destroy (this.gameObject);
        }
    }
}