- Home /
Help me with this code proplem,,Almost finish my game please help this once
hello people
the code here im trying to TurnOff an object called "Hidey" tagged as "Player" when it enters a trigger
I put the script inside a box with a boxcollider set to (Is Trigger)
The Proplem is the code turns off the object before it enters the trigger ,,,PLS HELP
using UnityEngine;
using System.Collections;
public class worldTrigger : MonoBehaviour {
private GameObject hidey;
void Start() {
hidey = GameObject.Find("hidey");
hidey.gameObject.SetActive(false);
}
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("Player")) {
hidey.gameObject.SetActive(true);
}
}
}
this script is so "Paradox" as it never does what it should do no matter how it might look well coded i almost give up on my game cuz this code
my game dependes on it greatly i need all the world energy xDDDD
Answer by Reynarz · Aug 04, 2017 at 12:31 PM
Almost all is fine, but your problem is that you are putting, true and false in wrong places.
Example:
void Start() {
hidey = GameObject.Find("hidey");
hidey.gameObject.SetActive(false); //This need to be true
}
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("Player")) {
hidey.gameObject.SetActive(true); //This need to be false
}
ive done this before and its not working,,,this time the Obj doesnt hide wether it enters the trigger or away from the trigger
anyleads?
Answer by a161803398874 · Aug 05, 2017 at 02:36 AM
@MTDues First... your code its flipped and needs to be as @Reynards said... second you need to make sure the collider you are using to detect the collision is set to "isTrigger" in the inspector window of the collider! It needs to be check marked or else the collider system ignores it!
Your script is also wrong because you want to dissapear hidey when it hits something with the tag "player" so instead you need to put the tag of the objects that are goin to hide hidey, in this example we will use the tag "ObjectsForHidding" any collider with this tag will make it him or her disapear.
using UnityEngine;
using System.Collections;
public class worldTrigger : MonoBehaviour {
private GameObject hidey;
void Start() {
hidey = GameObject.Find("hidey");
hidey.gameObject.SetActive(true);
}
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("ObjectsForHidding")) {
hidey.gameObject.SetActive(false);
}
}
}
The OP states "im trying to TurnOff an object called "Hidey" tagged as "Player" when it enters a trigger" (confusing I know) so it's the "Player" tag that needs to be tested in OnTriggerEnter, not "ObjectsForHidding".
nope you dont use player tag inside you need to use the tag of the object you want to make you hide
No, because this script is not attached to "hidey" (the player), it's attached to "a box" (which is neither the player nor the object to be hidden)
Answer by tanoshimi · Aug 04, 2017 at 12:15 PM
"The Problem is the code turns off the object before it enters the trigger"
That's because you've written this in Start():
hidey.gameObject.SetActive(false);
...
ive done this before and its not working,,,this time the Obj doesnt hide wether it enters the trigger or away from the trigger
anyleads?
Of course it won't hide - you're setting it to be inactive as soon as the game starts. So it won't do anything...
but when i edited it to this new code,, the proplem now is it wont hide XDD,,,as if there is no trigger enter if statement at all xD
using UnityEngine;
using System.Collections;
public class worldTrigger : $$anonymous$$onoBehaviour {
private GameObject hidey;
void Start() {
hidey = GameObject.Find("hidey");
hidey.gameObject.SetActive(true); //This need to be true (done)
}
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("Player")) {
hidey.gameObject.SetActive(false); //This need to be false (done)
}
}
}
Your answer
Follow this Question
Related Questions
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
Script executing through walls 1 Answer
Trying to find the highest number than add it to itself. 2 Answers
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer