71m3
71m3

Reputation: 345

void Start() not being called

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

Upvotes: 8

Views: 15475

Answers (2)

Péter Kovács
Péter Kovács

Reputation: 204

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

Upvotes: 1

Codemaker2015
Codemaker2015

Reputation: 15638

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.

Upvotes: 1

Related Questions