- Home /
set active object using c#
hello everyone in my game i have key if player collected that key a game object(finish object) should active i created a code in that code if the keys are 0 the game object is set active false but when the keys are collected game object is still false i will attach my script to you
public GameObject continued; private int keys;
// Use this for initialization void Start() { keys = BlueKeyText.KeyAmount; } // Update is called once per frame void Update() { if (keys == 1) { continued.SetActive(true); } else { continued.SetActive(false); } }
THIS script collects the key
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BlueCoins : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start()
{
Blue$$anonymous$$eyText.$$anonymous$$eyAmount = 0;
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag.Equals("Blue"))
{
// Sound$$anonymous$$anagerScript.PlaySound("Coin");
Blue$$anonymous$$eyText.$$anonymous$$eyAmount += 1;
Destroy(gameObject);
}
}
}
Please edit your question, select your code and click on the image with the ones and zeros to fix the styling. Unity answers already has an ugly design, please don't make it worse by poorly formatting your question. That way also your question will be more likely to be read by more users.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GreenWining : $$anonymous$$onoBehaviour {
public GameObject continued;
private int keys;
// Use this for initialization
void Start()
{
keys = Blue$$anonymous$$eyText.$$anonymous$$eyAmount;
}
// Update is called once per frame
void Update()
{
if (keys == 3)
{
continued.SetActive(true);
}
else
{
continued.SetActive(false);
}
}
}
Answer by ShadowUser19 · Dec 26, 2018 at 02:04 PM
I think what you are missing are colliders. If you have time, you might also want to take a look at the space shooter tutorial. Specifically the explosions part where a DestroyByContact script is created. I am sure it will be very useful.
What you need to do is to add a collider to both your player & the key. For most humanoid characters a sphere collider will work. For the key either a box or sphere might work as long as you don't forget to make the collider on the key a trigger collider. After adding colliders you also want to make sure you have a rigidbody attached to both the player & the keys. Also disable the gravity option if you don't use it. Otherwise things will fall through the floor. Lastly you need to use OnTriggerEnter to detect collision on the keys.
In my game I have a setup like this:
public enum Tags
{
Player,
MainCamera,
// etc...
}
// In my case this script is attached to some coins
public class IncreaseScoreByContact : MonoBehaviour
{
public int IncreaseValue;
// LevelSceneManager is a script I made to use methods like InscreaseScore
// & is attached on an empty object inside my scene
private LevelSceneManager _levelSceneManager;
private void Awake() {
_levelSceneManager = FindObjectOfType<LevelSceneManager>();
}
void OnTriggerEnter (Collider other)
{
// Here I could also have used if(other.tag == "Player"), but I prefer enums.
// In your case this could be something like Tags.Key
if(other.tag == $"{Tags.Player}")
{
// In your case this could be something like keys += 1
_levelSceneManager.IncreaseScore(IncreaseValue);
gameObject.SetActive(false);
}
}
}
i have a script for key collector and i will attach that script to you! and i have colliders for player and keys! & if i collect the key the score will update so that not a issue ; if the player collects the 3 key a finish object (finish line ) should be active ! if he didt collect the three key then the finish object(finish line ) should not be active!
(NOW I WILL ATTACH THE $$anonymous$$EY SCRIPT TO YOU)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BlueCoins : $$anonymous$$onoBehaviour {
// Use this for initialization void Start() { Blue$$anonymous$$eyText.$$anonymous$$eyAmount = 0; }
// Update is called once per frame void Update() {
}
void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.tag.Equals("Blue")) { // Sound$$anonymous$$anagerScript.PlaySound("Coin");
Blue$$anonymous$$eyText.$$anonymous$$eyAmount += 1; Destroy(gameObject); } } }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity5 filled my SSD? 0 Answers
Generate floor mesh according to walls 0 Answers