void Start()未被调用

时间:2014-04-28 07:19:54

标签: c# unity3d

我正在使用2D模式在Unity 4.3中制作游戏。但由于某种原因,在场景开始时没有调用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");
}

2 个答案:

答案 0 :(得分:0)

您只需复制该脚本的内容并将其删除。然后使用相同的代码创建一个新脚本并将其附加到gameObject。它将解决问题。

答案 1 :(得分:0)

我也一直在为此苦苦挣扎。通过设法将 Player设置/其他设置

底部的 Logging * 更改为 Full ,可以解决此问题