7
votes

I'm making a game in Unity 4.3 with 2D mode. But for some reason the void Start() function isn't being called on the start of the scene. I even attached a Debug.Log("Hello"); to the start function but it doesn't even do that so I know that the Start() function isn't being called. Although, the Update() function is called.

Here is the script.

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

You can see, there is an Update method which does work.

EDIT: Whole script:

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
Is your script's component enabled/ticked on the game object in the editor?Chris McFarland
Yes its ticked, Thanks @Chrisjharri
Have you tried to put the Debug.Log on the top of the Start function, just in case there is not animation and something bad happen on Stop animation line?kreys
Tried that, still not working @kreysjharri
The only possibility I see is something i dont know if its possible to happen: if you copied&paste that "Start" word from another place with a type of text codification and for some reason unity does not support it then "Start" is not write properly. Delete the function and retype each character again. (just a crazy thing i agree)Fabricio

2 Answers

1
votes

You just copy the contents of that script and delete it. then create a new script with same code and attach it to the gameObject. It will resolve the issue.

1
votes

I've been struggling with this also. I've managed to solve it by changing Logging* to Full in the bottom of Player Settings/Other Settings