2014-04-28 7 views
5

Ich mache ein Spiel in Unity 4.3 mit 2D-Modus. Aber aus irgendeinem Grund wird die void Start() Funktion nicht am Anfang der Szene aufgerufen. Ich habe sogar eine Debug.Log("Hello"); an die Startfunktion angehängt, aber das funktioniert nicht einmal, daher weiß ich, dass die Start() Funktion nicht aufgerufen wird. Allerdings wird die Update() Funktion aufgerufen.void Start() wird nicht aufgerufen

Hier ist das Skript.

void Start() 
{ 
    //Stop the animation 
    this.animation.Stop(); 
    Debug.Log ("Hello"); 
} 

// Update is called once per frame 
void Update() { 

Sie können sehen, es gibt eine Update-Methode, die funktioniert.

EDIT: ganze Skript:

using UnityEngine; 

public class Player : MonoBehaviour 
{ 
/***************************************************************************************************************** 
* PUBLIC VARIABLES 
* CAN BE CHANGED IN INSPECTOR WINDOW 
*****************************************************************************************************************/ 
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 VARIABLES 
* CANNOT BE CHANGED IN INSPECTOR WINDOW 
*****************************************************************************************************************/ 
private float speed = 10.0f; 

void Start() 
{ 
    Debug.Log("hi!"); 
    this.animation.Stop(); 
    Debug.Log ("Hello"); 
} 

// Update is called once per frame 
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 
void OnCollisionEnter2D(Collision2D other) 
{ 
    Die(); 
} 

void Die() 
{ 
    Application.LoadLevel(Application.loadedLevel); 
} 

void accelorometer() 
{ 
    /***************************************************************************************************************** 
    * TO GET ACCELOROMETER ANGLE 
    * AND MOVE JAYBIRD 
    *****************************************************************************************************************/ 

    // 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); 
} 

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"); 
} 
} 
+0

Ist die Komponente des Skripts auf dem Spielobjekt im Editor aktiviert/markiert? –

+0

Ja, es ist ein Häkchen gesetzt, Danke @Chris – SamuraiKitty

+0

Haben Sie versucht, 'Debug.Log' oben auf der Start-Funktion zu platzieren, nur für den Fall, dass keine Animation vorhanden ist und etwas Schlimmes in der Stop-Animationszeile passiert? – kreys

Antwort

0

Monobehaviour Life Cycle

Es ist nicht lösen Ihr Start() Methodenaufruf Problem. Sie können jedoch Awake() oder OnEnable() zum Starten des Skripts verwenden.

Verwandte Themen