Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void Start() not being called

Tags:

c#

unity3d

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");
}
like image 742
71m3 Avatar asked Apr 28 '14 07:04

71m3


People also ask

Is start called on disabled objects?

Start function works the same way as Awake but it cannot be called if the script is disabled. This also means that you can manually delay Start by disabling the game object and then enable it through another piece of code.

Is Start called in the editor unity?

Description. Start is called on the frame when a script is enabled just before any of the Update methods are called the first time. Like the Awake function, Start is called exactly once in the lifetime of the script.

Is Start called when an object is instantiated?

Start is always called immediately before the Update loop on the first frame the GameObject is active. So there is always a delay till the end of the current frame, longer if the GameObject is Instantiated inactive.


2 Answers

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.

like image 79
Codemaker Avatar answered Oct 22 '22 20:10

Codemaker


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

like image 44
Péter Kovács Avatar answered Oct 22 '22 19:10

Péter Kovács