Hide game object in other scenes in Unity - c#

I've used this code to stop destroying a specific game object. The problem is when i change the scene that contain this object to another different scene ,the game object still showing even in different scene...
How to only show the game object in its scene.
I hope that I clearly specify my problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DontDestroy : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(this.gameObject);
if(FindObjectsOfType(GetType()).Length > 1) {
Destroy(gameObject);
}
}
}

You can try doing something like this:
m_Scene = SceneManager.GetActiveScene();
sceneName = m_Scene.name;
//Do logic by checking the specific scene you want
if (sceneName == YOUR_SCENE) {
this.gameObject.SetActive(false); //Set false to hide, true to show
}

Related

How to set an inactive object active in Unity?

I am making a restart button in unity to restart the game, but the problem is that the button won't appear again after I have hidden it during the game.
Here is the code of the button:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class restartButtonScript : MonoBehaviour
{
public GameObject Player;
public Button restartButton;
public GameObject restartButtonObject;
// Start is called before the first frame update
void Start()
{
gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Player.GetComponent<playerScript>().playerEliminated == true)
{
gameObject.SetActive(true);
}
else if (Player.GetComponent<playerScript>().playerEliminated == false)
{
gameObject.SetActive(false);
}
}
}
If Game Object is inactive, its update won't be called, that's why you aren't seeing your button appear.
Turn off buttons graphics instead of disabling whole gameObject.
Alternatively you can can create an empty parent to your button, attach restartButtonScript to it, and turn off just child gameObject with button. gameObject with script will be active, so its update will be running as well.
Also, you could make it active from another script, but that's not needed there.

How can i un do a destroy() function in C#

I need to destroy an object, in my case is "Player2" witch is in the same scene as "Player1" and then after Player1 position.x is past "baraj".position.x reapear. Basically how can i make a temporarly destroy method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class switchCameras2 : MonoBehaviour
{
public GameObject baraj;
public GameObject CameraOne;
public GameObject CameraTwo;
public GameObject Player1;
public GameObject Player2;
public void Start()
{
CameraOne.SetActive(true);
CameraTwo.SetActive(false);
}
void Update()
{
if (GameObject.Find("Player1").transform.position.x > GameObject.Find("baraj").transform.position.x)
{
CameraTwo.SetActive(true);
CameraOne.SetActive(false);
}
if (GameObject.Find("Player1").transform.position.x < GameObject.Find("baraj").transform.position.x)
{
Destroy(Player2);
}
if (GameObject.Find("Player1").transform.position.x > GameObject.Find("baraj").transform.position.x)
{
Destroy(Player1);
}
}
}
If you want to Destroy it then you should make a new one with Instantiate method. If you don't want to Destroy it, one alternative is to deactivate it with SetActive like this Player1.SetActive(false); and then enable it like this Player1.SetActive(true);
Try creating a new GameObject variable called "U". And then right before destroying or in the start do U = player2.gameObject;
Now let's say you destroyed it and want to spawn. To do that we do
player2 = U.gameObject;
Instantiate(player2);
That will spawn player 2.
But as mentioned in the other answer .SetActive(false); would make things easier. Anyway if you still want to use destroy then that is how to do that. Good luck

Setting a bool active from a different script

I need to active icons in my UI. Since I make use of a pick up script I want to activate the object once I picked it up. I made a bool in my main script but I can't turn the bool on or off in my pick up script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NadePickUp : MonoBehaviour
{
public GameObject PUEffect;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PickUp(other);
// activate = true;
}
}
void PickUp(Collider2D player)
{
Shooting stats = player.GetComponent<Shooting>();
stats.shotType = "grenade";
GameObject effect = Instantiate(PUEffect, transform.position, Quaternion.identity);
PlayerStats activate = player.GetComponent<PlayerStats>();
activate = true;
Destroy(gameObject);
}
}
There are many answers to something like this on Unity Answers, here for example is one that answers your question.
If your PlayerStats is a class, you're treating it as a boolean. You're getting a reference to the script component and then immediately setting that value to true.
Assuming there's a public boolean activate on your PlayerStats class you could do something like this:
PlayerStats playerStats = player.GetComponent<PlayerStats>();
playerStats.activate = true;
This is based on pure assumption of your PlayerStats class.

Unity GameOver Screen

I am just trying to activate game over screen when the Player's activate is 'false'. There is no animation, just the
There are 3 objects that need to be active and i added the script to those 3 objects but the screen does not appear.
How can i fix?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverManager : MonoBehaviour
{
public GameObject _player;
void Start()
{
_player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
if (_player.activeInHierarchy == false)
{
gameObject.SetActive(true);
}
else
{
gameObject.SetActive(false);
}
}
}
My suspicion is the following. Whenever a Gameobject is not enabled, its code does not run. Test this by adding a Debug.Log("test") message.
If no message appears you can be certain that this check is never evaluated. To work around this simply add a script that is bound to an active gameObject. Creat a new empty gameobject in the scene. And add something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverManager : MonoBehaviour
{
public GameObject _player;
public GameObject _endscreen;
void Update()
{
if (_player.activeInHierarchy == false)
{
_endscreen.SetActive(true);
}
else
{
_endscreen.SetActive(false);
}
}
}
Assign the Variables in the Inspector by dragging the object to the empty fields. Never use GameObject.Find Methods.
If you need any further help tell me :)
What you're currently doing is to find a single GameObject and checking whether that is active or not.
It would make more sense, and be more optimized, if each player object adds himself to a list of all players when he spawns. You can then loop over that list instead to check all players.
An even better way would be if you only call that "GameOver" check after a player has died. So when you call whichever method kills him.
Based on Franz Answer, I did some modification in the code. You won't need any if else statement if you use a shortcut.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverManager : MonoBehaviour
{
public GameObject _player;
public GameObject _endscreen;
void Update()
{
_endscreen.SetActive(!_player.activeInHierarchy);
}
}

Unity 3D(C#) - How to connect more than two box colliders used as triggers

The current error : NullReferenceException: Object reference not set to an instance of an object BackboardTrigger.OnTriggerEnter (UnityEngine.Collider altCollider) (at Assets/Scripts/BackboardTrigger.cs:10)
I believe my question is different from, What is a NullReferenceException, and how do I fix it?, because it requires a narrow answer. The other post provides a broad scope of what a NullReferenceException is while I need to know how to connect more than two triggers.
I'm a novice in C# and I'm attempting to recreate a simple basketball game. As of now I'm trying to trigger a score for when the ball hits the backboard first and sequentially enters the hoop. The tutorial currently teaches how to trigger a score for when the ball begins to enter the hoop and reaches near the bottom of it.
Right now when the ball enters the hoop through the first trigger(PrimaryTrigger) and sequentially through the second(SecondaryTrigger) a score is triggered.
I'm trying to cause a different scoring action for when the ball hits the backboard first.
My PrimaryTrigger Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrimaryTrigger : MonoBehaviour {
void OnTriggerEnter(Collider collider)
{
SecondaryTrigger trigger = GetComponentInChildren<SecondaryTrigger>();
trigger.ExpectCollider(collider);
}
}
SecondaryTrigger Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SecondaryTrigger : MonoBehaviour {
Collider expectedCollider;
Collider possibleCollider;
public void ExpectCollider(Collider collider)
{
expectedCollider = collider;
}
public void PossibleCollider(Collider altCollider)
{
possibleCollider = altCollider;
}
void OnTriggerEnter(Collider otherCollider)
{
if(otherCollider == expectedCollider && otherCollider == possibleCollider)
{
ScoreKeeper scoreKeeper = FindObjectOfType<ScoreKeeper>();
scoreKeeper.IncrementScore(1);
}
else if(otherCollider == expectedCollider)
{
ScoreKeeper scoreKeeper = FindObjectOfType<ScoreKeeper>();
scoreKeeper.IncrementScore(2);
}
}
}
BackboardTrigger Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackboardTrigger : MonoBehaviour {
void OnTriggerEnter(Collider altCollider)
{
SecondaryTrigger newTrigger = GetComponent<SecondaryTrigger>();
newTrigger.PossibleCollider(altCollider);
}
}
Any form of help would be greatly appreciated. My first game in Unity.

Categories

Resources