Currently, my code allows the player to go to the next scene by clicking. I want to, however, make the fade out into the next scene animation automatic after 4 seconds. How can I do this?
I've tried looking up information, but nothing seems to work.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class LevelChanger : MonoBehaviour
{
// Start is called before the first frame update
float timer = 4f;
public Animator animator;
private int levelToLoad;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Invoke("FadeToLevel(1)", 2f);
}
}
public void FadeToLevel (int levelIndex)
{
levelToLoad = levelIndex;
animator.SetTrigger("FadeBlack");
}
public void OnF`enter code here`adeComplete()
{
SceneManager.LoadScene(levelToLoad);
}
}
The code works as intended, but I want the animation to happen automatically.
If I understood correctly, what you are aiming is to make an animation to play automatically when the player enters the new scene.
If that's the case, then you are looking for the sceneLoaded() method from SceneManager
Also, this discussion may be useful
Related
I've been using Unity to create a simple 2D game but the problem is that even when the game object "player" gets destroyed, the gameobject "isDead" (text) doesn't appear.
This is my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class youDied_Text : MonoBehaviour
{
private Transform player;
private Text isDead;
// public static bool isDead;
// Start is called before the first frame update
private void Start() {
isDead = GetComponent<Text>();
}
void checkForDeath()
{
if (player==false)
{
isDead.gameObject.SetActive(true);
}
else
{
isDead.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
player = GameObject.FindWithTag("Player").transform;
checkForDeath();
}
}
This script is attached in the text which I need to display in UI element.
As was noted currently you would get a NullReferenceException which is definitely not what you want.
There is absolutely no need / redundancy going through Transform at all actually. Simply store the GameObject reference instead
You are currently setting the object to inactive which has the Text attached ... which is the same object your component is attached to as well!
=> As soon as you end up in the second case once you set it to inactive => from now on Update is never called anymore!
In general as it sounds like this should only happen once anyway I would use a more event driven approach and have a component on your player like e.g.
public class Player : MonoBehaviour
{
public UnityEvent onDied;
private void OnDestroy ()
{
onDied.Invoke();
}
}
And then simply attach a listener/callback to that event once without poll checking states. You can do this either via the Inspector directly (just like in e.g. Button.onClick) or via code like e.g.
public class youDied_Text : MonoBehaviour
{
// Already reference things via the Inspector if possible!
[SerializeField] private GameObject player;
[SerializeField] private Text isDead;
private void Awake()
{
if(!isDead) isDead = GetComponent<Text>();
isDead.gameObject.SetActive(false);
// If you want to rather set it via the Inspector remove all the rest here
//if(!player) player = GameObject.FindWithTag("Player"). GetComponent<Player>();
// or even simpler
if(!player) player = FindObjectOfType<Player>();
player.onDied.AddListener(OnPlayerDied);
}
// If you want to rather set it via the Inspector make this public
private void OnPlayerDied()
{
isDead.gameObject.SetActive(true);
}
}
So in my game I have an object that I need to move smoothly from Vector3 fromPosition to Vector3 toPosition at speed float speed, and then go back to where it started. All very simple, but to try and make life easier when setting up levels I decided to make a custom inspector for this script with buttons that allow me to set the target positions to the current position of the object, so I can just move it to where it needs to be and click a button, rather that typing in all the coordinates. Thought I had it all working but then started seeing some very strange behaviour, after playing around it seems to be as follows: The first time a button is used all is well. Every time the button is used after that, the values change properly in the inspector, but upon hitting Play the values of toPosition and fromPosition are reverted to what they were the first time the button was used. (They don't revert back again on Stop). However if I type the values in manually, it works perfectly. Very strange, does anyone have any idea what might be happening here? The code for the script and custom inspector are bellow.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MovingGrapple : MonoBehaviour
{
public Vector3 fromPosition;
public Vector3 toPosition;
public float speed;
Rigidbody thisBody;
Grapple player;
// Start is called before the first frame update
void Start()
{
thisBody = GetComponent<Rigidbody>();
player = GameObject.Find("Head").GetComponent<Grapple>();
}
private void FixedUpdate()
{
thisBody.MovePosition(Vector3.MoveTowards(transform.position, toPosition, Time.fixedDeltaTime * speed));
if(transform.position == toPosition)
{
transform.position = fromPosition;
if (player.activeTarget != null && GetComponentsInChildren<Transform>().Contains(player.activeTarget.transform))
{
player.BreakGrapple();
GameObject.Destroy(player.activeTarget);
}
}
}
public void SetFromPosition()
{
fromPosition = transform.position;
}
public void SetToPosition()
{
toPosition = transform.position;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(MovingGrapple))]
public class MovingGrappleInspector : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MovingGrapple myTarget = (MovingGrapple)target;
if (GUILayout.Button("Set From position."))
{
myTarget.SetFromPosition();
}
if (GUILayout.Button("Set To position."))
{
myTarget.SetToPosition();
}
}
}
Thanks.
This will not only happen if you press play .. your changes are never saved!
If possible you should never mix editor scripts with direct access to the target except you know exactly what you're doing!
You would especially need to "manually" mark your changed object as dirty. Otherwise the changes are only temporary until your object is deserialized again (Enter/Exit PlayMode or reload of the Scene or asset).
You could before the changes add a Undo.RecordObject
if (GUILayout.Button("Set From position."))
{
Undo.RecordObject(myTarget, "SetFromPosition");
myTarget.SetFromPosition();
}
if (GUILayout.Button("Set To position."))
{
Undo.RecordObject(myTarget, "SetToPosition");
myTarget.SetToPosition();
}
Also (sounds unlikely in your use-case but)
Important: To correctly handle instances where objectToUndo is an instance of a Prefab, PrefabUtility.RecordPrefabInstancePropertyModifications must be called after RecordObject.
In general rather always go through SerializedProperty where possible like e.g.
[CustomEditor(typeof(MovingGrapple))]
public class MovingGrappleInspector : Editor
{
private SerializedProperty fromPosition;
private SerializedProperty toPosition;
private MovingGrapple myTarget
private void OnEnable()
{
fromPosition = serializedObject.FindProperty("fromPosition");
toPosition = serializedObject.FindProperty("toPosition");
myTarget = (MovingGrapple)target;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
// This loads the current real values into the serialized properties
serializedObject.Update();
if (GUILayout.Button("Set From position."))
{
// Now go through the SerializedProperty
fromPosition.vector3Value = myTarget.transform.position;
}
if (GUILayout.Button("Set To position."))
{
toPosition.vector3Value = myTarget.transform.position;
}
// This writes back any changes properties into the actual component
// This also automatically handles all marking the scene and assets dirty -> saving
// And also handles proper Undo/Redo
serializedObject.ApplyModifiedProperties();
}
}
I am working on a 3d basketball game in unity.
It has a score and a timer. After a certain time my scene loads and starts from the beginning. Every time I throw the ball, I have to spawn it.
It has a spawn button and a shoot button. But I don't want to use the spawn button. So I want to spawn the ball automatically.
How should I do it? I am giving my spawn button code and throw button code bellow.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnButton: MonoBehaviour
{
public GameObject ball;
public GameObject BallPosition;
public void Spawn()
{
ball.transform.position = BallPosition.transform.position;
var ballPosition = ball.transform.position;
ball.GetComponent<Rigidbody>().useGravity = false;
ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
ball.transform.position = ballPosition;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowButton: MonoBehaviour
{
static Animator anim;
public GameObject ball;
public float ballThrowingForce = 5f;
internal bool holdingBall;
void Start()
{
anim = GetComponent<Animator>();
ball.GetComponent<Rigidbody>().useGravity = false;
}
public void Throw()
{
anim.SetTrigger("isThrowing");
StartCoroutine(Test());
}
IEnumerator Test()
{
yield return new WaitForSeconds(1.5f);
ball.GetComponent<Rigidbody>().useGravity = true;
//ball.GetComponent<Rigidbody>().AddForce(transform.up * ballThrowingForce);
ball.GetComponent<Rigidbody>().AddForce(0, 380.0f, ballThrowingForce);
}
}
To spawn a ball you should create a prefab and instantiate it. Example:
private class Spawner : MonoBehaviour
{
public GameObject prefab;
public GameObject Spawn() => Instantiate(prefab);
}
So that your throw code should spawn a ball and if you want destroy an older one.
The above answer by Iv Misticos mentioned the use of Instantiate which is a great way to spawn a new ball.
before you spawn a new ball, you need to specify when it must spawn.
Either you can
use a Invoke("SpawnNewBallMethod",3) //after this piece of code is executed, it waits for 3 seconds and executes the method you mention in it which can have the code to instantiate.
Or After the first ball's work is done, you can destroy it(Destroy(ball)) or set active to false - Then check the state (if it exists(null check?) or gameObject.active==true) and accordingly instantiate a new ball (if it is setactive(false) don't forget to destroy it later).
Although gameObject.active is obsolete, it will serve the purpose without complicating things.
In my opinion, Invoke is better than a IEnumerator here as Invoke will not stop the execution flow and continue with the other things while the timer is running.
I'm going through the worse struggle right now. I'm trying to create a game with pitfalls that if the players fall through, they respawn and the game counts their death. I'm completely at wit's end and don't know where to even start with this. The game is 3D
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
public Transform spawnPoint;
public float minHeightForDeath;
public GameObject player;
void Start () {
}
// Update is called once per frame
void Update () {
if (player.transform.position.y < minHeightForDeath) {
player.transform.position = spawnPoint;
}
}
}
Use a collider on a plane/cube/whatever works for you, and set it to be a trigger. So when they pass through the trigger collider, it can record them and do what you want with it.
Use the OnTriggerEnter method script
void OnTriggerEnter(Collider other)
{
other.transform.position = spawnPoint.position;
}
You are not putting any data in the "minHeightForDeath" float. Unless you do it in the editor.
Other than that, have you put the script on the player gameObject? And have you set what you have to in the editor?
How can I check if a specific animation has finished playing in Unity, then execute an action? [C#] I am not using an animator.
From: http://answers.unity3d.com/questions/52005/destroy-game-object-after-animation.html
To execute an action from the animation editor...
-Create a script with a simple public function that will destroy the object. e.g.
public class Destroyable : MonoBehaviour
{
public void DestroyMe()
{
Destroy(gameObject);
}
}
-Add that script to the animated object you want to destroy.
-In the animation editor, move the animation scrubber to the end of the animation.
-Use the 'Add Event' button in the animation toolbar
-Select 'DestroyMe' from the function drop-down in the Edit Animation Event dialog.
-Now your animation should play, run the 'DeleteMe' function, and destroy the object/do your action.
I've used this method a few times, comes in handy for certain things in animations :)
You should check Animation.IsPlaying value.
From the docs:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Animation anim;
void Start() {
anim = GetComponent<Animation>();
}
void OnMouseEnter() {
if (!anim.IsPlaying("mouseOverEffect"))
anim.Play("mouseOverEffect");
}
}
Just as Andrea said in his post : Animation-IsPlaying is pretty much what you need since you don't use Animator. Check Animation to see other sweet stuff you can use.
using UnityEngine;
using UnityEngine.Collections;
public class ExampleClass : MonoBehaviour
{
Animation anim;
void Start()
{
anim = GetComponent<Animation>();
}
//In update or in another method you might want to check
if(!anim.isPlaying("StringWithAnimationClip") //or anim.clip.name
//Do Something
}
You can also force stop the animation with anim.Stop();
Now you commented that you don't want to use isPlaying() so if you can please elaborate I will edit my post.