Problem In Animation On Brackeys Tutorial - c#

I've been following Brackeys's tutorial on how to make a video game in Unity3D. Up to now things have been great.
But now when I create a game over screen and animated it, it keeps on looping when it triggers. I've tried turning off loop time and transitioning to an empty state with no exit time. It also doesn't loop the whole thing just around less than half a second.
My EndTrigger script and CompleteLevel script is below. I don't get any debugging errors.
using unityEngine;
public class EndTrigger : MonoBehaviour {
public GameManager gameManager;
void OnTriggerEnter ()
{
gameManager.CompleteLevel();
}
}
My CompleteLevel Script Below
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelComplete : MonoBehaviour {
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}

In your project files select the animation file and make sure you've turned off Loop Time. Unity enables it by default.

Related

This Unity Scene is not loading

I don't know why my scene is not loading, can I get help?
When I press the button nothing happens.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class gamecontroller : MonoBehaviour
{
public Text points;
public void Start() {
points.text = Score.scoreval + " POINTS";
}
public void Restart() {
SceneManager.LoadScene("SampleScene");
Score.scoreval = 0;
}
public void Menu()
{
SceneManager.LoadScene("Menu");
Score.scoreval = 0;
}
}
Reason might be that you did not assign the scene in your build. This is an easy fix if this is the case. To check if it is, you can go to the Build Settings... under the File tab. Once in there, you should see the Scenes In Build category, in there you should also see your scene(s). If not, you will need to assign them by going to each scene and pressing Add Open Scenes. Once done, it should work properly from there.
Not sure if this is an issue, it has been a while since I have coded in C#. But, in my scene manager, I wrote the scenes name as such:
public void StartButton()
{
SceneManager.LoadScene(labScene);
}
Not sure if that matters, but I thought I might as well put that out there.
There seems to be a bug from Unity side as it has happened with me sometimes that whenever I'm trying to load the scene using the LoadScene method by passing the SceneName as the parameter, Unity throws up an error even when those scenes have been added in the Build Settings.
Removing the scenes from the build settings and adding them again is what worked for me. :)

For some reason GameObject.Find().SetActive(true) doesn't work and I am unsure why

In my game my player has 2 weapons and in this level I want to make it so that once the player runs out of ammo the shotgun deactivates and the axe activates. Both weapons are child's of the player but this script is attached to the player. The shotgun deactivates but the axe doesn't activate and I don't know why. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ammo : MonoBehaviour
{
public int ammo = 165;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(ammo == 0)
{
GameObject.Find("wp_shotgun").SetActive(false);
GameObject.Find("wp_axe").SetActive(true);
}
}
}
Take into account that a GameObject may be inactive because a parent is not active. In that case, calling SetActive will not activate it, but only set the local state of the GameObject, which you can check using GameObject.activeSelf. Unity can then use this state when all parents become active. Check the documentation.
What I would do is check the hierarchy in case there is some gameobject messing with the desired GO activation, and second check if there is any error in the console in case the GameObject.Find() could throw a nullreference exeption.

Unity Script is not running

i am creating all new unity project and added Empty GameObject in to my first Scene, and also created and added script to that object, but when i'm trying to run my scene script dose not showing any output in my console window.
here is my code
using System.Collections;
using UnityEngine;
public class ScriptObject : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method called.");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update method calling.");
}
}
You need to turn on showing of another types of log messages ("Info" messages in your case).
You need to be sure that your button of the console output is enabled. Just try to click on the button.Look at the screenshot

How to add listener to UnityEvent in prefab programatically

I read about UnityEvent and am trying to implement it.
I have a "GameController.cs" script, attached to a GameObject "ScriptsHolder", and a "target.cs" script attached to a Prefab GameObject called "target".
"target" is not in the scene on startup: I initiate multiple targets at a certain point, which are renamed to "target0", "target1", ...
When a target receives damage, I want it to send its name to GameController.cs, which has a void that destroys that object.
Here's what I did:
target.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class target : MonoBehaviour {
public UnityEvent damageEvent;
void Damage(float damage)
{
damageEvent.Invoke();
}
}
Next, I click on my target Prefab and in the inspector, I try to add "ScriptsHolder" to the Damage Event. Here's my problem: I can't drop the ScriptsHolder in the Damage Event. Did some testing, and the reason for this appears to be the fact that "target" is not yet in my scene.
So I read about adding a listener instead, but I can't figure out how to do this.
Here what I have in "GameController.cs":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class GameController : MonoBehaviour {
UnityEvent damageEvent;
void Awake()
{
damageEvent.AddListener(targetReceivedDamage);
}
void targetReceivedDamage()
{ // do some stuff }
}
But that doesn't work. What am I doing wrong? Or how can I add the ScriptsHolder to the Damage Event even though "target" isn't inside the scene?
Any help is much appreciated!
You need to add listener when you instantiate target object. For example:
Change your prefab declaration to Target type and reassign target prefab in the inspector.
public Target targetPrefab;
then
Target target = Instantiate(targetPrefab);
target.damageEvent.AddListener(targetReceivedDamage);
Hope this helps :)

Simple log in unity c# script

It should be simple, but still can't find a straight answer:
How can I log a simple message using C# Unity script?
I tried this:
Debug.Log("Hello Log");
It doesn't work or I'm not looking at the right place.
Debug.Log("Hello Log"); can be found on the Console Tab.
For you to be able to see it:
1.You must put it in a function from a script.
public class Test : MonoBehaviour
{
void Start()
{
Debug.Log("Hello Log");
}
}
2.Script must be attached to a GameObject.
3.Both the script and the GameObject it is attached to must be enabled.
4.Reset the Layout if you can't see the Console tab.
EDIT:
I tested it on Android Device. That's why I didn't see it.
In that case, use Android Monitor from Android Studio to view the log from your Android device.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Simple : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Attach this script to component and simple message will be there when you start project");
}
// Update is called once per frame
void Update()
{
}
}

Categories

Resources