I came to an issue when I try to trigger the event that must turn on the block moving animation, that i set as "move" in animator, it doesn't go to this state, but when I manually trigger it it works completly fine. You can check a screenshot of an animator and ask me to screenshot anything else to solve the problem.
public class BlockScript : MonoBehaviour
{
[SerializeField] private Animator blockanim;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
blockanim.SetTrigger("move");
}
}
}
Well the problem was solved by myself here is what i changed in code and now it works completely fine I didn't notice that I used box collider 2D
using System.Collections.Generic;
using UnityEngine;
public class BlockScript : MonoBehaviour
{
[SerializeField] private Animator blockanim;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
blockanim.SetTrigger("move");
Debug.Log("MOVE");
}
}
}
Related
I am making a shooting game in Unity, and I am using particles as the laser/bullets. Even when I press left alt, the particles don't show up. It does print out firing weapon on the console, but the particles don't appear. I am using c#.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fire : MonoBehaviour
{
[SerializeField] ParticleSystem weapon;
// Start is called before the first frame update
void Start()
{
weapon.Pause();
}
// Update is called once per frame
void Update()
{
WeaponProcessor();
}
void WeaponProcessor()
{
if (Input.GetButtonDown("Fire2"))
{
if (!weapon.isPlaying)
{
Debug.Log("Firing weapon");
weapon.Play();
}
}
else
{
weapon.Pause();
Debug.Log("Not firing weapon");
}
}
}
By the way, weapon.pause does not work either.
ParticleSystem.Pause freezes particles, this doesn't seem to be the correct behavior for weapon fire.
Pauses the system so no new particles are emitted and the existing particles are not updated.
You need to restart Particle system like this:
using UnityEngine;
public class Fire : MonoBehaviour
{
[SerializeField] private ParticleSystem fireParticles;
private void Start()
{
if (fireParticles.isPlaying)
{
fireParticles.Stop();
}
}
private void Update()
{
WeaponProcessor();
}
private void WeaponProcessor()
{
if (Input.GetButtonDown("Fire2"))
{
fireParticles.Play();
}
if (Input.GetButtonUp("Fire2"))
{
fireParticles.Stop();
}
}
}
Already fired bullets will not stop and new ones will be created on the next "Fire2" press
Using Unity 2021.3.16f1.
I followed this tutorial from Brackeys to make the pause menu for my flappybird like game, my player movement stops working after I exit the main game to the main menu and return to the game.
Part of player code responsible for movement:
public class birdScript : MonoBehaviour
{
public Rigidbody2D myRigidbody;
public float flapStrength;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
myRigidbody.velocity = Vector2.up * flapStrength;
FindObjectOfType<AudioManager>().Play("jump");
}
}
}
Part of pause menu code responsible for loading the main menu scene:
using UnityEngine.SceneManagement;
public class pauseMenu : MonoBehaviour
{
public static bool gameIsPaused = false;
public GameObject pauseMenuUI;
public void loadMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Title");
}
}
Part of code responsible for loading the main game scene:
using UnityEngine.SceneManagement;
public class playButton : MonoBehaviour
{
public void loadLevel()
{
SceneManager.LoadScene("Main game");
}
}
I tried changing the play button code that loads the main game, in the scene hierarchy my main menu scene is 0 and my main game scene is 1. The play button is on the main menu.
I turned this
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
Into this
SceneManager.LoadScene("Main game");
But nothing happened, I tried searching on google but I have no idea how to search for the right answers.
Your problem has nothing to do with scene loading. That said your code for loading level is fine as it was:
using UnityEngine.SceneManagement;
public class playButton : MonoBehaviour
{
public void loadLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Issue could be in your pauseMenu script but you only provided a part of the code. You need to make sure your Time.timeScale is set to 1f when the level scene is loaded.
I would suggest setting the default value for flapStrength. Also in void Start there is Time.timeScale set to 1f just to be sure. You could do this in the pauseMenu script.
public class birdScript : MonoBehaviour
{
public Rigidbody2D myRigidbody;
public float flapStrength = 10f;
void Start()
{
Time.timeScale = 1f;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
myRigidbody.velocity = Vector2.up * flapStrength;
FindObjectOfType<AudioManager>().Play("jump");
}
}
}
I hope the answer helped. The issue is that the problem could be anywhere. It is necessary to go through all the values and examine the behavior of the player. Didn't an error appear that would stop the game? If the problem persists, you would need to edit the question and add more information.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
For some reason when I'm in the normal view in-game I am able to link the scripts that I need to call in order to make an animation like so
however, whenever I start the game it automatically removes them from the slots, and it doesn't work
I am completely clueless as to why this may be happening and unity keep giving me errors that say that I'm not setting an instance to my script I really have no clue why this may be happening.
I have 3 scripts that I'm working with that is giving me the problem
one is the main script for the enemy vision (I am referencing the other scripts in this one)
the second is the enemy animation script which makes him go from idle to attack and the third is an animation for the gun model since I had to make it follow the hands of the enemy
scripts attached bellow
1st script(enemyAI):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAi : MonoBehaviour
{
public bool detected;
GameObject target;
public Transform enemy;
public GameObject Bullet;
public Transform shootPoint;
public float shootSpeed = 10f;
public float timeToShoot = 1f;
public EnemyAni Animation;
public GunAni GunAnimation;
void Start()
{
Animation = GetComponent<EnemyAni>();
GunAnimation = GetComponent<GunAni>();
}
public void Update()
{
//makes the enemy rotate on one axis
Vector3 lookDir = target.transform.position - transform.position;
lookDir.y = 0f;
//makes enemy look at the players position
if (detected)
{
enemy.LookAt(target.transform.position, Vector3.up);
enemy.rotation = Quaternion.LookRotation(lookDir, Vector3.up);
}
if (detected == true)
{
Animation.LookPlayer = true;
GunAnimation.ShootPlayer = true;
}
if (detected == false)
{
Animation.LookPlayer = false;
GunAnimation.ShootPlayer = false;
}
}
//detects the player
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
detected = true;
target = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
detected = false;
}
}
}
2nd Script (EnemyAnimation):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAni : MonoBehaviour
{
public Animator animator;
public bool LookPlayer;
public void Start()
{
animator = GetComponent<Animator>();
}
public void Update()
{
if (LookPlayer == true)
{
animator.SetBool("IsShootingStill", true);
}
else
{
animator.SetBool("IsShootingStill", false);
}
}
}
3rd script (GunAnimation):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunAni : MonoBehaviour
{
public Animator animator;
public bool ShootPlayer;
public void Start()
{
animator = GetComponent<Animator>();
}
public void Update()
{
if (ShootPlayer == true)
{
animator.SetBool("IsShooting", true);
}
else
{
animator.SetBool("IsShooting", false);
}
}
}
Your code:
void Start()
{
Animation = GetComponent<EnemyAni>();
GunAnimation = GetComponent<GunAni>();
}
Searches the GameObject that holds the EnemyAI script for EnemyAni and GunAni. If the GameObject doesn't have those it will return null.
Sounds like you want to remove that code.
Note that if the scripts exist on a child of that GameObject you will need to use GetComponentInChildren
There are some things to review, good practices:
GameObject target is private for C#, but it is better to put private before.
variable names like Bullet Animation GunAnimation should always begin with lowercase, because they might be confused with a Class Name (search in internet for CamelCase and PascalCase).
Name should be clear. EnemyAni Animation is not clear enough, because Animation maybe any animation (player, enemy, cube, car, etc.).
It is better enemyAnimation to be clear, as you did with GunAnimation (only just with lower case at beginning)
As slaw said, the Animation must be in the GO attached.
about last item
Let's say you have an empty GO (GameObject) called GameObject1 and you attach EnemyAi script to it.
In Runtime (when game mode begins), it will try to find Animation = GetComponent<EnemyAni>();, but it won't find it
Why?
Because GetComponent<> searches for the Component(Class) that is in the <> (in this case EnemyAni) in its GO (in this case, GameObject1), but the unique script attached to GameObject1 is EnemyAI.
So, you have 3 options:
Attach EnemyAni to GameObject1
Create another GO (for example, GameObjectEnemyAni), attach the script EnemyAni and drag and drop GameObjectEnemyAni to GameObject1 and delete Animation = GetComponent<EnemyAni>(); in Start
Keep in mind: if you leave that line of code, instead of getting the script EnemyAni from GameObjectEnemyAni, it will run the code Animation = GetComponent<EnemyAni>(); in Start, and obviously, it won't find it
Create events. It's a really good practice for avoiding code tight coupling, but that is more advanced stuff.
i faced the problem that after some manipulations with code(Dont remember after what exactly) Physics2D.IgnoreCollision stopped working properly. I'm new to stack overflow, and dont know what else to attach here, if something else is needed to understand the situation, please tell me and i will add. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnOffGroundCollider : MonoBehaviour
{
[SerializeField] GameObject ground;
[SerializeField] GameObject ball;
private void OnTriggerEnter2D(Collider2D collision)
{
Physics2D.IgnoreCollision(ball.GetComponent<CircleCollider2D>(),
ground.GetComponent<BoxCollider2D>(), true);
}
private void OnTriggerExit2D(Collider2D collision)
{
Physics2D.IgnoreCollision(ball.GetComponent<CircleCollider2D>(),
ground.GetComponent<BoxCollider2D>(), false);
}
}
I'm trying to have an if condition to do something if the sprite renderer of another object is enabled, but it won't work.
Here's the code I tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUpReCharge : MonoBehaviour
{
public Animator anim;
public Animator animc;
public Animator anime;
public GameObject neon;
public GameObject chargesprite;
public AudioSource recharge;
public BoxCollider2D collision;
public SpriteRenderer blackout;
public AudioSource ambient;
public AudioSource music;
void Start()
{
anime.Play("Pickup", 0, 1f);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
neon.GetComponent<PlayerMovement>().enabled = true;
chargesprite.GetComponent<SpriteRenderer>().enabled = false;
collision.GetComponent<BoxCollider2D>().enabled = false;
anim.SetBool("IsDead", false);
anim.Rebind();
animc.Rebind();
anime.Rebind();
recharge.Play();
Destroy(gameObject, 3.0f);
//activate blackout
blackout.GetComponent<SpriteRenderer>().enabled = false;
if (blackout.enabled)
{
ambient.Play();
music.Play();
}
}
}
}
The public blackout has a sprite renderer that is disabled. When enabled, I want the last bit of code to run but it won't.
What's wrong with it?
Prodian asked if I tried gameobject.activeself, and I want to thank you for the advice. It works perfectly! I changed the reference type to gameobject and reaasigned it and made the necessary and it works like a charm now!
Here's the full new code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUpReCharge : MonoBehaviour
{
public Animator anim;
public Animator animc;
public Animator anime;
public GameObject neon;
public GameObject chargesprite;
public AudioSource recharge;
public BoxCollider2D collision;
public GameObject blackout;
public AudioSource ambient;
public AudioSource music;
void Start()
{
anime.Play("Pickup", 0, 1f);
}
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
neon.GetComponent<PlayerMovement>().enabled = true;
chargesprite.GetComponent<SpriteRenderer>().enabled = false;
collision.GetComponent<BoxCollider2D>().enabled = false;
anim.SetBool("IsDead", false);
anim.Rebind();
animc.Rebind();
anime.Rebind();
recharge.Play();
Destroy(gameObject, 3.0f);
if (blackout.gameObject.activeSelf)
{
ambient.Play();
music.Play();
}
blackout.gameObject.SetActive(false);
}
}
}
EDIT: One more thing to note! The location of the if statement WAS important. It should have been before the code that makes the sprite false. This means the previous code would've worked if that had been done, but the gameobject code is much better for me because the blackout layer is one that I need to be OFF all the time.
Basically, my old code works! Just move the if statement before the "getcomponent......enabled = false" because the script is read in order.
Your blackout is SpriteRenderer so calling GetComponent on it is invalid. Change that to:
blackout.enabled = true;
Or use:
anotherGameObject.GetComponent<SpriteRenderer>().enabled = true;
Calling SetActive on a GameObject will disable not only the SpriteRenderer component! In some cases this changes your desired behaviour.