How can I enable/disable mesh renderer of multiple gameobjects when the player enters in a collider? This is my code but it doesn't works.
using UnityEngine;
using System.Collections;
public class SueloManager : MonoBehaviour {
private GameObject suelo;
void Start ()
{
suelo = GameObject.FindGameObjectsWithTag ("SueloWireframe");
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player") {
suelo.GetComponent<Renderer> ().enabled = false;
Debug.Log ("Oculta suelo");
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player") {
suelo.GetComponent<Renderer> ().enabled = true;
Debug.Log ("Aparece suelo");
}
}
}
FindGameObjectWithTag returns a single GameObject and FindGameObjectsWithTag returns array of GameObject. Just like Kroltan mentioned, you have to change suelo to an array then use loop to enable and disable all of them. Having the loop in a simple re-usable function should simplify this. Look at the EnableRenderer function in the solution below.
private Renderer[] sueloRenderers;
void Start()
{
GameObject[] suelo = GameObject.FindGameObjectsWithTag("SueloWireframe");
sueloRenderers = new Renderer[suelo.Length];
for (int i = 0; i < sueloRenderers.Length; i++)
{
sueloRenderers[i] = suelo[i].GetComponent<Renderer>();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
EnableRenderer(sueloRenderers, false);
Debug.Log("Oculta suelo");
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
EnableRenderer(sueloRenderers, true);
Debug.Log("Aparece suelo");
}
}
void EnableRenderer(Renderer[] rd, bool enable)
{
for (int i = 0; i < rd.Length; i++)
{
rd[i].enabled = enable;
}
}
Related
is actually a rythm game and when a circle enter into a trigger we can make disapear the circle.
public class CursorPress : MonoBehaviour {
public bool canBePressed;
public KeyCode keyToPress;
void Update()
{
if (Input.GetKeyDown(keyToPress))
{
GameObject target = GameObject.Find("hitcircle_r");
if(target.tag == "Target")
{
if (canBePressed)
{
target.GetComponent<Animation> ().play("hitcircle_fading");
target.SetActive(false);
}
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Target")
{
canBePressed = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == "Target")
{
canBePressed = false;
}
}
}
I want that when the player press a key (keyToPress), a custom gameObject (hitcircle_r) gets a fade animation before disapearing, but idk how to do it, i tried a lot of code i found on the web, can someone helps me please ?
There are many ways to achieve what you want.
One would be to call a coroutine that fades deactivates your target after time.
If you are only doing fading style animations you should look into DOTween.
using System.Collections;
using UnityEngine;
public class CursorPress : MonoBehaviour
{
public bool canBePressed;
public KeyCode keyToPress;
void Update()
{
if (Input.GetKeyDown(keyToPress))
{
GameObject target = GameObject.Find("hitcircle_r");
if (target.tag == "Target")
{
if (canBePressed)
{
target.GetComponent<Animation>().Play("hitcircle_fading");
float hideTime = 0.5f; // the length of your animation
StartCoroutine(DeactivateTarget(target, hideTime));
}
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Target")
{
canBePressed = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == "Target")
{
canBePressed = false;
}
}
// coroutine to deactivate GameObject
private IEnumerator DeactivateTarget(GameObject targetGo, float hideDelay)
{
yield return new WaitForSeconds(hideDelay);
targetGo.SetActive(false);
}
}
When my character hits the gameobject collider, an enemy will be spawned and the collider is disabled, cuz I do not want to spawn multiple enemies. When my character dies and I have to start from the beginning, the collider should be enabled again to spawn the enemy again.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerSpawner : MonoBehaviour
{
public EnemySpawn enemyspawn;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
enemyspawn.SpawnEnemy();
gameObject.GetComponent<BoxCollider2D>().enabled = false;
}
}
}
//in other class
private void SetHealth(float health)
{
var actualNextHealth = Mathf.Min(m_maxHealth, health);
m_currentHealth = actualNextHealth;
if (m_healthBar != null && m_maxHealth > 0f)
m_healthBar.SetHealth(actualNextHealth / m_maxHealth);
if (m_currentHealth <= 0f)
{
UpdateHighscore();
Die();
}
}
private void Die()
{
m_character.NotifyDied();
if (m_canRespawn)
{
SetVulnerable();
RemovePoison();
m_hazards.Clear();
gameObject.transform.position = m_spawnPosition;
SetHealth(m_maxHealth);
}
else {
Destroy(gameObject);
}
}
You can create a static variable in the trigger script that you assign the Collider value to it.
When an enemy is spawned it deactivates, as in your code.
public class TriggerSpawner : MonoBehaviour
{
public static Collider2D spawnCollider;
public EnemySpawn enemyspawn;
void Start() => spawnCollider.GetComponent<Collider2D>();
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
enemyspawn.SpawnEnemy();
spawnCollider.enabled = false;
}
}
}
When you die, it will reactivate.
private void Die()
{
m_character.NotifyDied();
if (m_canRespawn)
{
TriggerSpawner.spawnCollider.enabled = true;
SetVulnerable();
RemovePoison();
m_hazards.Clear();
gameObject.transform.position = m_spawnPosition;
SetHealth(m_maxHealth);
}
else {
Destroy(gameObject);
}
}
With minimal changes on your code, I'd suggest this:
private void Die()
{
m_character.NotifyDied();
if (m_canRespawn)
{
SetVulnerable();
RemovePoison();
m_hazards.Clear();
gameObject.transform.position = m_spawnPosition;
SetHealth(m_maxHealth);
gameObject.GetComponent<BoxCollider2D>().enabled = true; // add this line
}
else {
Destroy(gameObject);
}
}
I Am Working With Unity 3d and know little scripting and unity well. I Came To a point where I don't know what to do. My OnCollisionExit Don't Work on collision exit there is moment = true this dont work. Somebody told me you have not reset the variable. So I Don't Know How To Make It So Please Help. Thanks In Advance
This is The Player Script This Is My Script or code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Player : MonoBehaviour
{
public Animator anim;
public Transform housepos;
public Transform deadpos;
public static bool safe1_click = false;
public bool safe1_clicked = false;
public float speed = 25;
public Rigidbody rb;
Collider mycollider;
float Walk = 8;
float jump = 7;
float sadWalk = 6;
float idle = 0;
public static bool stopwalking = false;
public static bool Jump = false;
public static bool sadwalk = false;
public static bool freeze = false;
public static bool yourchance = false;
public static bool movement = true;
public bool movement23 = true;
// Start is called before the first frame update
void Start()
{
mycollider = transform.GetComponent<Collider>();
rb = GetComponent<Rigidbody>();
anim.SetFloat("Animation", Walk);
stopwalking = false;
Jump = false;
freeze = false;
}
// Update is called once per frame
void Update()
{
movement23 = movement;
if (movement == true)
{
safe1_click = safe1_clicked;
rb.velocity = new Vector3(speed * Time.deltaTime, rb.velocity.y, 0);
}
else
{
rb.velocity = new Vector3(0,0,0);
}
if (Jump == true)
{
anim.SetFloat("Animation", jump);
}
if (Jump == true)
{
anim.SetFloat("Animation", sadWalk);
}
if (stopwalking == true)
{
anim.SetFloat("Animation", idle);
}
}
public void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "stop")
{
Debug.Log("Stopped");
spawner.stopspawn = true;
movement = false;
return;
}
if (collision.gameObject.tag == "dec")
{
Debug.Log("oh");
freeze = true;
}
}
public void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "stop")
{
Debug.Log("Spawning");
spawner.stopspawn = false;
movement = true;
}
if (collision.gameObject.tag == "dec")
{
Debug.Log("oh");
freeze = false;
}
}
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "fre")
{
spawner.yourchance = true;
}
if (other.gameObject.tag == "Player")
{
Destroy(other.gameObject);
}
if (other.gameObject.tag == "dec")
{
freeze = true;
Debug.Log("oh");
}
if (other.gameObject.tag == "new")
{
yourchance = true;
Debug.Log("stopstop");
}
if (other.gameObject.tag == "con")
{
Debug.Log("collide happened");
movement = true;
}
}
private void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "dec")
{
freeze = false;
Debug.Log("oh");
}
}
}
I'm currently working on some type of LOD system where I have some ferns that have to be enabled and disabled. I'm using a cube for this process, so when when my character touches the cube the ferns meshrenderers will be disabled and when not touching it, it'll enable them. Now the part where I struggle, is when I have to enable/disable the meshrenderes of these gameobjects tagged with fern.
Here is my code so far:
public GameObject[] FernPlants;
public MeshRenderer MR;
// Start is called before the first frame update
void Start()
{
FernPlants = GameObject.FindGameObjectsWithTag("Fern");
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
//Disable meshrenderers on FernPlants
}
}
private void OnTriggerExit(Collider other)
{
//Enable meshrenderers on FernPlants
}
Try with something like:
public GameObject[] FernPlants;
public MeshRenderer MR;
// Start is called before the first frame update
void Start()
{
FernPlants = GameObject.FindGameObjectsWithTag("Fern");
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
//Disable meshrenderers on FernPlants
foreach(GameObject go in FernPlants)
{
go.GetComponent<MeshRenderer>().enabled = false;
}
}
}
private void OnTriggerExit(Collider other)
{
//Enable meshrenderers on FernPlants
foreach(GameObject go in FernPlants)
{
go.GetComponent<MeshRenderer>().enabled = true;
}
}
I'm working in a mobile game that has multiples areas in the same scene. Each area has a trigger and when the player enters in it several objects are spawned to pick up them. How can I do to deactivate a picked up object so when the player enters in this area again this object doesn't spawn again?
This is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PathologicalGames;
public class InOutZone_ZONAS: MonoBehaviour {
//Objetos
[Header("Objetos")]
public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;
private GameObject[] despawnObjects;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
SpawnObjectsZ ();
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
GameObject[] despawnObjects = GameObject.FindGameObjectsWithTag("ItemZona");
for (int i = 0; i < despawnObjects.Length; i++)
{
PoolManager.Pools ["Objetos"].Despawn (despawnObjects[i].transform);
Debug.Log("Despawnea Objetos");
}
}
}
void SpawnObjectsZ()
{
foreach (GameObject spawnPosition in spawnPositions) {
int selection = Random.Range (0, spawnObjects.Count);
PoolManager.Pools ["Objetos"].Spawn (spawnObjects [selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
}
}
}
How can I do to deactivate a picked up object so when the player
enters in this area again this object doesn't spawn again?
There are many ways to do this. You can simply remove the GameObject from the List after you spawn it with spawnObjects.Remove(spawnObjects[selection]);
public class Playervitals : MonoBehaviour
{
//Objetos
[Header("Objetos")]
public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;
private GameObject[] despawnObjects;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
SpawnObjectsZ();
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
GameObject[] despawnObjects = GameObject.FindGameObjectsWithTag("ItemZona");
for (int i = 0; i < despawnObjects.Length; i++)
{
PoolManager.Pools["Objetos"].Despawn(despawnObjects[i].transform);
Debug.Log("Despawnea Objetos");
}
}
}
void SpawnObjectsZ()
{
for (int i = 0; i < spawnPositions.Count; i++)
{
GameObject spawnPosition = spawnPositions[i];
int selection = Random.Range(0, spawnObjects.Count);
PoolManager.Pools["Objetos"].Spawn(spawnObjects[selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
spawnObjects.Remove(spawnObjects[selection]);
}
}
}