【发布时间】:2019-11-20 08:01:05
【问题描述】:
我在 Unity 4.3 中使用 2D 模式制作游戏。但由于某种原因,void Start() 函数在场景开始时没有被调用。我什至将 Debug.Log("Hello"); 附加到 start 函数,但它甚至没有这样做,所以我知道 Start() 函数没有被调用。虽然,Update() 函数被调用了。
这是脚本。
private void Start()
{
this.animation.Stop();
Debug.Log("Hello");
}
您可以看到,有一个 Update 方法确实有效。
编辑:整个脚本:
public class Player : MonoBehaviour
{
public Vector2 jumpForce = new Vector2(0, 300);
public Vector2 moveForce = new Vector2(0, 300);
public Vector2 sideForce = new Vector2 (250, 0);
public GameObject obstacle;
public float scrollSpeed = 30;
public AnimationClip fly;
public GameObject player;
private float speed = 10.0f;
private void Start()
{
Debug.Log("hi!");
this.animation.Stop();
Debug.Log("Hello");
}
private void Update() {
onTouch();
int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
/*if (fingerCount > 0)
{
player.rigidbody2D.velocity = Vector2.zero;
player.rigidbody2D.AddForce (moveForce);
}*/
try
{
player.rigidbody2D.velocity = Vector2.zero;
player.rigidbody2D.AddForce (moveForce);
}
catch(UnityException e)
{
Debug.Log("Fail");
}
if (Input.GetKeyDown ("right"))
{
player.rigidbody2D.velocity = Vector2.right;
player.rigidbody2D.AddForce (sideForce);
}
accelorometer();
}
// Die by collision
private void OnCollisionEnter2D(Collision2D other)
{
Die();
}
private void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
private void accelorometer()
{
// Get the accelerometer data:
Vector2 acceleration = Input.acceleration;
// Get the forward value from one of the three channels in the acceleration data:
float translation = acceleration.x * speed;
// Make it move 10 meters per second instead of 10 meters per frame
translation *= Time.deltaTime;
// Move translation along the object's z-axis
player.transform.Translate (translation, 0, 0);
}
private void onTouch()
{
/*int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce (moveForce);
}
if (fingerCount > 0)
{
player.rigidbody2D.velocity = Vector2.zero;
player.rigidbody2D.AddForce (moveForce);
}*/
if(Input.GetKeyDown ("up"))
{
Debug.Log("ghjkl");
player.rigidbody2D.velocity = Vector2.zero;
player.rigidbody2D.AddForce (moveForce);
}
//print("User has " + fingerCount + " finger(s) touching the screen");
}
【问题讨论】:
-
您的脚本组件是否在编辑器中的游戏对象上启用/勾选?
-
是的,谢谢@Chris
-
你有没有试过把
Debug.Log放在开始函数的顶部,以防没有动画和停止动画行发生不好的事情? -
试过了,还是不行@kreys
-
我看到的唯一可能性是我不知道它是否可能发生:如果你从另一个地方复制并粘贴了“开始”这个词,并且由于某种原因统一不支持它然后“开始”没有正确写入。删除函数并重新键入每个字符。 (我同意这只是一件疯狂的事情)