1

我访问另一个脚本,但她的值默认为空。

我想在与障碍物(+5)碰撞时增加分数。

代码:

球员得分.cs

public static class playerscore            
{
    public static int Score = 0;   //static variable
}

TouchControll.cs

public Text scoretext;

void Awake()
{
   //when I game quit and reenter the game then display the last score
   PlayerPrefs.GetInt("score", playerscore.Score);
   
   //this data on my ram not in harddisk 
   playerscore.Score = PlayerPrefs.GetInt("score", playerscore.Score); 

   //last score text update when I Reenter the game
   scoretext.text = PlayerPrefs.GetInt("score").ToString();
   print(scoretext.text);
   
   //pass a score one scene to another scene
   PlayerPrefs.SetInt("score", playerscore.Score);
   scoretext.text = ("" + playerscore.Score);
   PlayerPrefs.SetInt("score", playerscore.Score);
}

void OnCollisionEnter2D(Collision2D col)
{
     //Debug.Log("oncollisionenter");
     //when my player reaches the finish line then the score add=100; //work fine
     if (col.gameObject.tag == "successfinishtag")
      {
          playerscore.Score += 100;
          PlayerPrefs.SetInt("score", playerscore.Score);    
          scoretext.text = ("" + playerscore.Score);
          //Debug.Log("scoreadd:" + playerscore.Score);
     }
}

问题在这里我想要当我的玩家与障碍物相撞然后增加我的分数(+5)但问题不是增加我的分数

障碍物.cs

int incrementscore = 5;

TouchControll touchcontroll; 

void Start()
{
   GetComponent<TouchControll>();
}
//here player colliding with the obstacle
void OnTriggerEnter2D(Collider2D other) 
{
    foreach (Transform child in transform) 
    {
            //Debug.Log("Inside foreach");
            
            if (child.tag == "obstacleobject")
            {
                Animator anim = gameObject.GetComponent<Animator>();

                anim.Play("animone");
            }
   }
   playerscore.Score = PlayerPrefs.GetInt("score", playerscore.Score);
   print(playerscore.Score);  
   Debug.Log(touchcontroll);  //null

   if (touchcontroll!= null)//null by default if(null != null) condition false
   {
            IncrementScore();
            

            touchcontroll.scoretext.text = playerscore.Score.ToString();
            
    }
}

    void IncrementScore()
    {
        //Debug.Log("Inside Increment score");
        playerscore.Score = playerscore.Score + incrementscore;

        PlayerPrefs.GetInt("score", playerscore.Score);
        //print(PlayerPrefs.GetInt("score", playerscore.Score));

        PlayerPrefs.SetInt("score", playerscore.Score);

        touchcontroll.scoretext.text = playerscore.Score.ToString();
    }

我想要当我的玩家与障碍物碰撞时增加我的分数(+5),但问题不是增加我的分数如何解决问题请帮忙。

如何解决这个问题呢??

4

3 回答 3

0

我在我的游戏中使用了这种场景,玩家与硬币碰撞,在我的情况下它会平稳地增加硬币价值。而且我还注意到我的脚本与您的脚本有简单的区别。你必须使用 FindObjectOfType<> 并且还必须调用 public int incrementscore = 5; U 没有使用任何访问修饰符。下面还包括我的代码,你可以看看并得到想法。希望这对你有用。谢谢

public class CoinScript : MonoBehaviour
{

public int coinValue;
private LevelManager gameLevelManager;

// Start is called before the first frame update
void Start()
{
    gameLevelManager = FindObjectOfType<LevelManager>();
}

// Update is called once per frame
void Update()
{

}

void OnTriggerEnter2D(Collider2D other) {
    Debug.Log("Triggered");
    if (other.tag == "Player")
    {
        gameLevelManager.AddCoins(coinValue);
        Destroy(gameObject);
    }

}

}

//在LevelManager脚本中

public void AddCoins(int numberOfCoins)
{
    coins += numberOfCoins;
    coinText.text = "Coins : " + coins;
}
于 2019-09-09T13:02:23.920 回答
0

您没有将 GetComponent 的结果分配给任何东西。更改障碍.cs:

TouchControll touchcontroll; 

void Start()
{
   touchcontroll = GetComponent<TouchControll>();
}

但是,GetComponent()仅当当前游戏对象附加了任何对象的实例时才返回某些内容。障碍物.cs 和 TouchControll.cs 是否都附加到当前游戏对象?

如果不是 GetComponent 不起作用,请改用 FindObjectOfType() :

void Start()
{
   touchcontroll = FindObjectOfType<TouchControll>();
}

以下是 Unity 脚本 API 的相关页面以获取更多信息:

https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html

于 2019-09-09T07:30:56.477 回答
-1

简单地说,没有使用 PlayerPrefs 到其他地方

public static class playerscore            
{
    public static int Score
    {
        get { return PlayerPrefs.GetInt("score", 0);} 
        set { PlayerPrefs.SetInt("score", value); }
    }
}
于 2019-09-09T09:38:17.307 回答