我有2个脚本,一个是目标控制器,另一个是时间控制器。 我希望在得分为10时增加时间,没有错误发生但画布中的时间没有增加,这是代码。
using UnityEngine;
using System.Collections;
public class target_controller : MonoBehaviour
{
public GameObject explosion;
public Transform center_transform;
public int score_made = 8;
void Start ()
{
// cal_board_score score_transform_script = this.gameObject.GetComponent<cal_board_score> ().center_trans ;
// center_transform = this.gameObject.GetComponent<cal_board_score> ().center_trans as Transform;
center_transform = this.transform;
}
void Update ()
{
// print ("a");
}
public int bullet_hit_at( Vector3 hit_point )
{
// int score = this.gameObject.GetComponent<cal_board_score> ().get_scores (hit_point);
return score_made;
//********* perform what ever you want **************
}
public void bullet_hit_score( int score )
{
InGameGUI gui_script = GameObject.FindGameObjectWithTag ("ingamegui").GetComponent<InGameGUI> () as InGameGUI;
gui_script.add_score ( score );
time_controller time_script = GameObject.FindObjectOfType (typeof(time_controller)) as time_controller;
Instantiate (explosion, transform.position, transform.rotation);
if (score == 10)
{
//gui_script.add_time(10.0f);
time_script.add_time (10.0f); // here i am adding score
}
else if (score <= 8)
{
if( GameObject.FindGameObjectWithTag("interactive_sound")!=null )
{
GameObject.FindGameObjectWithTag("interactive_sound").SendMessage("play_bottle_smash");
}
}
Destroy (this.gameObject);
//****** do whatever to destroy this this object
}
}
////////////第二个剧本
using UnityEngine;
using System.Collections;
public class time_controller : MonoBehaviour
{
//private datamemebers
private bool is_game_ended = false;
private float total_time = 90.0f ;
private float init_time;
private float end_time;
private InGameGUI gui_script;
private array_start_pos env_meta_script;
private float time_change_at;
void Start ()
{
init_time = Time.time;
if( GameObject.FindGameObjectWithTag("ingamegui")!=null )
{
gui_script = GameObject.FindGameObjectWithTag("ingamegui").GetComponent<InGameGUI>() as InGameGUI;
}
env_meta_script = GameObject.FindGameObjectWithTag ("env").GetComponent<array_start_pos> () as array_start_pos;
total_time = env_meta_script.total_time;
end_time = init_time + total_time;
time_change_at = total_time - 1.0f;
}
// Update is called once per frame
void Update ()
{
if(is_game_ended)
{
return;
}
total_time = end_time - Time.time;
if (total_time <= time_change_at)
{
gui_script.time_remain = total_time;
gui_script.update_time_gui ();
time_change_at = time_change_at - 1.0f;
}
if( total_time<=0.0f )
{
is_game_ended = true;
}
}
public void add_time(float time_to_add)
{
total_time= end_time+ time_to_add;
}
public void time_finished()
{
//do what ever you want to end the game
if(!is_game_ended)
{
is_game_ended = true;
if( GameObject.FindGameObjectWithTag("game_status")!=null )
{
GameObject.FindGameObjectWithTag("game_status").SendMessage("end_the_game");
}
}
}
}
public void update_gui()
{
add_bullets_text.text = add_bullets.ToString ();
score_reqscore_text.text = current_score.ToString () + "/" + total_score_req.ToString ();
remain_bullets_text.text = bullet_remaining.ToString ();
}
///这是方法
public void update_time_gui()
{
if (is_game_ended)
{
return;
}
string time_to_show = Mathf.Floor((time_remain/60.0f)).ToString("00")+":"+(time_remain%60).ToString("00");
time_remain_text.text = time_to_show;
if (time_remain <= 0.0f)
{
manager_script.game_lost_called ();
}
if (time_remain <= 10.0f)
{
time_remain_text.color = Color.red;
}
}
答案 0 :(得分:2)
首先你称之为:
public void add_time(float time_to_add)
{
total_time= end_time+ time_to_add;
}
在更新中,您再次更改它:
total_time = end_time - Time.time;
所以如果我理解正确你想要做的事情:
public void add_time(float time_to_add)
{
end_time += time_to_add;
}