Show Canvas by trigger in Unity3d - c#

I have a scene with two cubes, and canvas. One cube falls onto another. Canvas appearing must be triggered by collision. It's does not working. Where's the problem?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void Start()
{
}
void OnGUI()
{
GUICanvas.gameObject.SetActive(true);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "FallingCube")
{
OnGUI();
}
}
void Update()
{
}
}

Put the code below inside your EverythingInside script. Make sure Collider and Rigidbody are attached to both cubes. Also make sure to drag your Canvas from the Editor to the GUICanvas slot.
public Canvas GUICanvas;
void Start()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("FallingCube"))
{
GUICanvas.gameObject.SetActive(true);
}
}
void Update()
{
}
Unity Physics tutorials.
Unity UI Tutorials.
Unity Scripting Tutorials.

Your problem is that OnGUI() as a function is called once every frame in Unity, regardless of whether or not you call it manually. Also, OnGUI is reserved for doing things like rendering simple buttons and text directly to the screen, it doesn't perform any actual logic.
All you have to do to make this work is just replace where you've called OnGui() with GUICanvas.gameObject.SetActive(true)
and get rid of the other functions.
using UnityEngine;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void OnCollisionEnter(Collision col)
{
if (col.tag == "FallingCube")
{
GUICanvas.gameObject.SetActive(true);
}
}
}
That right there is literally all you need to make it work. Bear in mind that if you change it later on to be a trigger instead of two colliding cubes you will need to replave OnCollisionEnter with OnTriggerEnter.

Related

OnCollision Enter not displaying a debug log

using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
void onCollisionEnter (){
Debug.Log("We hit something.");
}
}
I have rigidbody on every object and I attacked the script to my object, but still not working. I am following a Brackeys tutorial and it is a little old so I don't know if something has changed about the syntax.
void OnCollisionEnter(Collision collision) {
}

Find the 'Number' of the Scene the Player is in to Save it in PlayerPref Unity

I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}

The local function 'OnTriggerEnter2D' is declared but never used

I don't know how to fix it. It's a 2D game. When you collide with the box, it should load/teleport you to the next scene, but it doesn't.
I tried everything on the internet and it didn't work.
using UnityEngine;
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour
{
public void LoadNextLevel()
{
void OnTriggerEnter2D(Collider2D collider)
{
SceneManager.LoadScene(1);
}
}
}
I expect it to teleport me to my next level.
It looks like you nested the OnTriggerEnter2D() function inside of LoadNextLevel()? That's what the Local Function warning is referring to.
Those should be two separate functions, not one within the other. OnTriggerEnter2D() is a function of MonoBehavior; the MonoBehavior (VictoryZone) is what gets notified of the collision.
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour {
public void LoadNextLevel() {
SceneManager.LoadScene(1);
}
void OnTriggerEnter2D(Collider2D collider) {
LoadNextLevel();
}
}
Note: you might also need to check the GameObject associated with collider to make sure that it's a player, and not an enemy or something (if, hypothetically, you had enemies or projectiles or other objects with colliders moving into the victory zone).

Unity2D How to ignore collisions with specific objects?

I am using an animated sprite to collide and delete an enemy sprite. The enemy sprite disappears when it hits my floor (sprite) also. (All have rigidbody2D). How do I get the enemy sprite to ignore the floor and everything else except the animated sprite?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision : MonoBehaviour
{
void Start()
{
}
void Update()
{
}
void OnCollisionEnter2D()
{
if (gameObject.tag.Equals("Enemy"))
{
Destroy(gameObject);
}
}
}
Go to Edit > Project Settings > Physics (or Physics2D) and edit the Layer Collision Matrix:

When I reload my game scene the objects do not reload in unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
Scene scene;
private void Awake()
{
scene = SceneManager.GetActiveScene();
}
public void LoadScene(string level)
{
if (level == "Game")
{
SceneManager.LoadScene("Game");
}
else
{
UnityEngine.SceneManagement.SceneManager.LoadScene(level);
}
}
}
When it reloads the game scene the objects are stationary like when they were when the scene finished.
What you do is supposed to work.
The only objects that are not reloaded are those that have a script containing DontDestroyOnLoad. You should check if this is the case for your objects
By the way, your if-else blocks are useless, they do the same thing as if you wrote
public void LoadScene(string level)
{
SceneManager.LoadScene(level);
}
If the objects are showing in the Scene view but not in the hierarchy, you should check if the scene is collapsed. If so, click the arrow to the left of the scene name.

Categories

Resources