簡體   English   中英

使用Unityscript讀取鼠標單擊Input.GetMouseDown

[英]Read mouse click Input.GetMouseDown with Unityscript

我嘗試繼續進行鼠標操作,找到了一個示例,但它不起作用,這是我的錯誤Assets / scripts / onclickx.js(13,5):BCE0044:期待EOF,找到了“}”。

這是我的代碼

import UnityEngine.UI;
import UnityEngine.EventSystems;



if(Input.GetMouseDown(0){
   // Whatever you want it to do.
   ScoreSystem.drinks -= 1;

   mySlider = GameObject.Find("water").GetComponent(UnityEngine.UI.Slider);
   counter.water = counter.water += 10
  mySlider.value = counter.water; 
}

這是counter.js的腳本

import UnityEngine.UI;
var mySlider: UnityEngine.UI.Slider;
var water = 90;
function Start () {
  // substitute 'sliderName' with the literal name 
  // of your slider as it appears in the hierarchy:
  mySlider = GameObject.Find("water").GetComponent(UnityEngine.UI.Slider);
  mySlider.value = water; 
  }


 function OnTriggerEnter(other : Collider) 
 {
        Destroy(gameObject);
        ScoreSystem.drinks += 1;
        mySlider.value += 10; 
  // insert desired variable here in place of 'n'
  // slider will automagically adjust to display new value
  }

這是我的分數系統的代碼

static var myScore = 0;
static var score = 0;
static var money = 0;
static var level = 0;
static var drinks = 0;


public var guiSkin : GUISkin;




function OnGUI()
{
 GUI.skin = guiSkin;

GUI.contentColor = Color.green;

GUI.Label(Rect((Screen.width / 2) - 60,15, 200, 30), "Score: " + score);
GUI.Label(Rect((Screen.width / 2) - 60,30, 200, 30), "Money: " + money);
GUI.Label(Rect((Screen.width / 2) - 60,42, 200, 30), "Level: " + level);
GUI.Label(Rect((Screen.width / 2) - -320,25, 200, 30), "Drinks: " + drinks);

}

您的第一個代碼只有很多問題。

1。代碼不在函數內。 那應該在Update函數中。

2。您在if語句中缺少多余的) ,因為GetMouseDown函數應該有一個) ,而另一個則關閉if語句。

3。未聲明mySlider變量。 您要在counter腳本中聲明它,但不在第一個腳本中聲明。

import UnityEngine.UI;
import UnityEngine.EventSystems;

var mySlider:Slider;

function Update(){

    if(Input.GetMouseDown(0)){
        // Whatever you want it to do.
        ScoreSystem.drinks -= 1;

        mySlider = GameObject.Find("water").GetComponent(UnityEngine.UI.Slider);
        counter.water = counter.water += 10;
        mySlider.value = counter.water; 
    }
}

注意:

最好在班級/腳本名稱中使用首字母大寫。 例如, counter應為Counter ,變量名應以小寫字母開頭。 這將使人們更容易閱讀和理解您的代碼。

最后,將所有腳本轉換為C# 您將必須盡快執行此操作,以便在刪除Unityscript編譯器時不必再次重新啟動項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM