Trying to disable a wall when th enemy is killed - unity3d

just finishing off my demo scene but im faced with an issue, when the enemy boss is killed (disabled) i need the wall behind him to be disabled too automatically. However i cant figure it out, im horrible with script and intend to do a crashcourse youtube tutorial over my december break lol.
But right now i dont understand how thi sis supposed to work, ive done something similar where an item is destroyed upon coming into contact with the player which disables a wall further along in the game:
public class potionpickup : MonoBehaviour
{
public Collider2D _FF;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
_FF.enabled = false;
//this will immediately change the color to the transparent.. and only for that specific object
Renderer renderer = _FF.GetComponent<Renderer>();
renderer.material.color = Color.clear;
Destroy(gameObject);
}
}
And that works, but i cant seem to use the same method for this and googling it is getting me nowhere. Any help would be appreciated :D
for reference, the enemy is just called Enemy and the wall is called PassageWay
What i tried:
about 30 formats of script were tried each with about 2-6 errors i couldnt understand how to fix like "have you misused an assebly reference?" and im like "yeah, probably, got a solution or you just gonna tell me im wrong?"

Related

Unity3D - How can I detect collison between my car object and terrain?

I am working on a driving game for a project. My issue is that I'm unable to detect collision (I want to place the car to a checkpoint whenever it goes off the road) between the wheels of my car and the terrain. I tried to use this simple script but it does not seem to work:
using UnityEngine;
public class CarCollision: MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
if (other.collider.name == "Terrain")
{
Debug.Log("We got off the road!");
}
}
}
Is it a 2d game? If yes, use OnCollisionEnter2D instead of OnCollisionEnter.
Is the script attached to the gameobject?
Are you sure that the terrain has a collider which is not marked as trigger?
Try copying and pasting this same thing in the main controller.
I hope it worked. Don't forget to mark my answer as accepted if it worked!

Unity | MissingReferenceException on scene reload

So I have a simple local multiplayer game using the new input system. There's the game scene and the results scene. Once the game ends, the results scene is loaded and you can press "start" to restart the game (load the game scene again).
The problem is, after I reload the game scene and then trigger an InputAction (seems to be only on context.started):
MissingReferenceException while executing 'PlayerInput.onActionTriggered' callbacks
MissingReferenceException: The object of type 'Rigidbody2D' 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.
The weird thing is, none of these supposedly missing references are actually missing. The players still move using the "missing" rigidbody and etc.
I apologize if I don't explain well. I'm new to Unity and quite lost. I'm happy to share screenshots or snippits of my code I honestly just don't know where to look for this.
You should make null all action events in start
Term
This is a common problem for developers using Unity. This is a problem that occurs so often that Unity Techonology has already given it a term to explain it.
'Fake Null'
unity blog link
https://blog.unity.com/technology/custom-operator-should-we-keep-it
In short, this happens because Unity's Object is a wrapper class that indicate pointer.
Code of Conduct
You can understand the details by reading the article on the Unity blog.
Unity developers need to be careful when using C#'s nullable syntax. (?, ??...etc)
Example Code
public class MyClass : Monobehaviour
{
[SerializeField] private Rigidbody _rigidbody;
private void DoSomething()
{
// This can cause Runtime error especially when GameObject that has
// Rigidbody already been Destroyed.
_rigidbody?.CallSomeMethod();
// This is Defensive Code preventing Runtime error
if (_rigidbody != null)
{
_rigidbody.CallSomeMethod();
}
}
}

Problem by collision with OnCollisionEnter2D class

I have problem with a simple spike script. I have attached the following script to the spike.
View it works also. The spikes are used in a jump down game. After the player has already jumped down several platforms the script is resolved even if the player does not touch the spikes.
public class CollisionSpikes : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D other)
{
SceneManager.LoadScene(0);
}
}
Here is a picture of how the spikes and their BoxCollider2D are connected to each other.
image
I first suspected when all colliders touched each other that this would lead to this error. But the problem still occurred. Here is a video so you can get an impression of the error:
youtube video
Does anyone here have an idea how I can fix this problem? I am relatively new to Unity and C# and unfortunately I am stuck here.
Thanks for your help. :)
Try to place a Debug.Log(other) to find out what is colliding with your spikes. Because you are not filtering it anything that touches it will trigger that code. My advice to you is to filter it. Use some tags.
Do something like
void OnCollisionEnter2D(Collision2D other)
{
Debug.Log(other); // Find out what is triggering the function
if(other.gameObject.tag == "player") {
SceneManager.LoadScene(0);
}
}

OnMouseOver works differently on same objects

I am a beginner in Unity and I just found out a behaviour I do not understand...
I have a prefab "cell" that I made from a sprite and I want it to change its color when my mouse is over it.
So I added a BoxCollider2D component to it as well as the following script:
public class Cell : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
private void OnMouseOver()
{
GetComponent<SpriteRenderer>().color = Color.magenta;
}
private void OnMouseExit()
{
GetComponent<SpriteRenderer>().color = Color.black;
}
}
Then when I drag and drop the "cell" prefab to the scene, it won't work (when my mouse is over the cell, nothing happens).
Same problem when I add another "cell" prefab to the scene.
But when I add a third "cell" prefab to the scene, the feature works on the 2 first cells but not on the third.
I probably missed something or there is a behaviour I do not know, anyway if someone knows why this is happening, please tell me.
Thanks!
I just tested your code in my game using a 2d box sprite and it works fine.
Video > https://youtu.be/6GP3-aV9g3g
You may want to try a couple of things to debug it.
First make sure that there is a BoxCollider2D and Rigidbody2D attached.
Make sure that there is nothing covering the boxes in the scene.
When I am having trouble with an aspect of the game I try to break it down into its simplest components. Try making a scene with nothing in it apart from the box and trying it, if that doesent work, try attaching the scrip to a non prefab object.
Try adding Debug.Log("Mouse Enter"); to the subs to check if the mouse is detected on enter, if it is detecting the mouse, maybe your spite renderer isn't working properly.
Try these things and let me know if they don't work, I would be happy to keep trying to figure it out.

Unity Game Stops On Load Same Scene Again

I have 7 scenes in my game. one of the scenes is "Playing" scene witch is my game play scene. in this game, one match includes 3 round. i save some of match data in "MatchManager" script that attached to MatchManager game object that have a singleton and don't destroy on load. other info are in a script(PSceneManager) that attached to a gameObject(PSceneManager) belong to "Playing" scene.
In the end of each round i change some data and then I Have To Load "Playing" scene again.
HERE IS MY PROBLEM: when it loads again it stops at very beginning and even don't enter "start" function (method).
(I have a singleton in PSceneManager scrip. I delete that and even check and looking for any other static value.(there were no more static variables))
I HAVE NO IDEA WHAT IS MY PROBLEM??!(any idea can be helpful)
HEEELP PLEASE...I'M STUCKS HERE...
I don't know exactly what the problem is. Some more information would be helpful.
If I would have had this problem I would look for the first and last moment the script still works.
Unity has already build this in for us. Just like Start() and Update() we have OnEnable() and OnDisable(). So if you add a Debug.Log() statement in those MonoBehaviour functions you know if the script and object are still active.
void OnEnable() { Debug.Log( gameobject.name + " is enabled" ); }
void OnDisable() { Debug.Log( gameobject.name + " is disabled" ); }
https://docs.unity3d.com/Manual/ExecutionOrder.html
I have faced the same problem, and I found that before the loading of the other scene again I changed the Time.timeScale to zero.
I solved the problem by reassigning its value in the start of any game object of the scene that stops.