如何从另一个脚本变量分配值?

时间:2019-09-09 07:13:13

标签: c# unity3d

我正在访问另一个脚本,但是默认情况下她的值为null。

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

代码:

playerscore.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),但问题不是增加我的分数

obstacle.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),但问题不是增加我的得分,如何解决问题。

如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

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

TouchControll touchcontroll; 

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

但是,GetComponent()仅在存在附加到当前游戏对象的任何对象的实例的情况下才返回内容。是否将barrier.cs和TouchControll.cs都附加到当前的GameObject?

如果不是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

答案 1 :(得分:0)

我在游戏中使用了这种情况,即玩家与硬币碰撞,在我的情况下,它使硬币的价值平稳增加。而且我还注意到您的脚本与我之间存在简单的区别。 U必须使用FindObjectOfType <>,还必须调用public int增量,scores = 5;其中U没有使用任何访问修饰符。下面还包括我的代码,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;
}

答案 2 :(得分:-1)

简单地说,不要在其他地方使用PlayerPrefs

public static class playerscore            
{
    public static int Score
    {
        get { return PlayerPrefs.GetInt("score", 0);} 
        set { PlayerPrefs.SetInt("score", value); }
    }
}