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");
}
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With