Some coding help needed
Hey everyone, I am fairly new to coding and am looking for some help on a few issues.
Basically I am making a game thats an endless runner and am having issues with
On start I have a countdown animation, but I can not get my object to wait for 4 seconds before taking off down the map. The only controls on the Player is left and right. It is coded so that it continues down the map automatically and the user will only have to swipe left or right to control the object.
I also cannot figure how to play an exploding animation when it crashes. It just disappears and the game goes back to the scene and says MissingRefererenceException
Any help would be greatly appreciated! Thanks everyone for looking!
What have you tried for the "object must wait" - you added some tags but no code to know how you are doing that. Also include the code/line# (relevant section only please) that produces the $$anonymous$$issingRef
I have not tried that. I added the code to the last response if you would like to take a look at what I have so far
Answer by sumitb_mdi · Dec 13, 2016 at 04:08 AM
To wait for 4 seconds, what you can do is make your Time.timeScale = 0, this will pause all the scripts. And start a Coroutine with WaitForSecondsRealtime, (Don't use WaitForSeconds as this is Time.timeScale dependent - so not good for your case )
void Start() { Time.timeScale = 0; StartCoroutine(InitialWait(4.0f)); //Time you want to wait in seconds. } private IEnumerator InitialWait(float waitTime) { yield return new WaitForSecondsRealtime(waitTime); Time.timeScale = 1.0f; }
You can use existing assets from Asset store for your exploding animations. There are tons of freely available, just explore. Have a look at : Freely Available assets.
Thank you for the response! I tried adding the code, but I ended up with multiple errors. Here is the code I am working with for the player controller. I also have Constant Force added to the game object so that it will automatically travel down the map in the Z direction. $$anonymous$$aybe this is my issue?
using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour
{
void Start()
{
}
void Awake()
{
ourVehicle = GetComponent<Rigidbody>();
}
Rigidbody ourVehicle;
public float speed;
public float tilt;
public float power;
void FixedUpdate() {
float moveHorizontal = Input.GetAxis("Horizontal");
Vector3 movement = new Vector3(moveHorizontal, 2.0f);
ourVehicle.velocity = movement * speed;
ourVehicle.rotation = Quaternion.Euler(ourVehicle.velocity.z, 0.0f, ourVehicle.velocity.x * -tilt
);
}
void OnCollisionEnter(Collision target)
{
if (target.gameObject.tag == "Lethal")
{
Destroy(gameObject);
}
if (target.gameObject.tag == "Diamond")
{
Destroy(target.gameObject);
}
}
}
Can you paste the errors too that you are getting while executing these scripts.
There isnt an error at the start. The issue is that the object takes off down the map during countdown. I have an animation that starts a countdown, 3, 2, 1, GO! I am trying to get it to takeoff after the animation.
The error I get on crashing into an object is this -
$$anonymous$$issingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. CameraController.LateUpdate () (at Assets/Scripts/CameraController.cs:27)
I am trying to play an explosion on crash but it just exits the scene once I hit something.
The error says it all, what is your Code at line number 27 in CameraController.cs ?? You are surely accessing a destroyed object. If you want I can help you directly on a Skype Call, that would be quick. (SkypeID : sumitb.mdi )
Update -
I figured out the wait time for 4 seconds code and it is working! There is a 4 second delay on start of the game, but there is one issue. The countdown is also paused and starts playing after the 4 seconds. How do I make the countdown play during the 4 second wait?
Thanks for the responses! I have tried adding the Time Scale to the Start section of the code and it ended up producing tons of errors. Here is the code I have for the player controller. I have added Constant Force to the game object to get it to travel down the map automatically. $$anonymous$$aybe that is the issue I am running into?
As far as the explosions go, I have an explosion from the assest store, but when I try and code it to play it is not waiting the time to play the animation. I am getting the error in the camera follow script, I can post that as well if needed.
using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour
{
void Start()
{
}
void Awake()
{
ourVehicle = GetComponent<Rigidbody>();
}
Rigidbody ourVehicle;
public float speed;
public float tilt;
public float power;
void FixedUpdate() {
float moveHorizontal = Input.GetAxis("Horizontal");
Vector3 movement = new Vector3(moveHorizontal, 2.0f);
ourVehicle.velocity = movement * speed;
ourVehicle.rotation = Quaternion.Euler(ourVehicle.velocity.z, 0.0f, ourVehicle.velocity.x * -tilt
);
}
void OnCollisionEnter(Collision target)
{
if (target.gameObject.tag == "Lethal")
{
Destroy(gameObject);
}
if (target.gameObject.tag == "Diamond")
{
Destroy(target.gameObject);
}
}
}