How to reference a GameObjects Box Collider true in a if statement!
I can not seem to find a way to check if that is the correct game objects box collider.
What I want to happen is when my "Player" enters a box collider [That is a Trigger] that has this script component attached to it, I want it to double check that it is indeed the player, then searched for the right box collider.
I made a reference to the game object then enabled it on awake, and then in the if statement I wanted it to make sure it was that box collider
Thank You for your Help
--Not posting full script here as it would take up unnecessary room----
private bool playerCollCheck = false;
//GameObject
private GameObject battery; // Referance to the GameObject with the BoxCollider on it
private GameObject safeRoomKey;
//Collider
private BoxCollider safeRoomboxCollider;
private BoxCollider batteryboxCollider;
void Awake()
{
battery = GameObject.Find("BatteryController"); // Find the gameObject with the BoxCollider
safeRoomKey = GameObject.Find("SafeRoomKeyController");
}
void OnTriggerStay(Collider coll)
{
if (GameObject.Find("Player").GetComponent<myCharacterController>().itemTaken)
{
PlayerCheck(coll);
if (playerCollCheck == true)
{
mainKeyTextController();
batteryTextController();
}
}
}
void mainKeyTextController()
{
if (safeRoomKey.GetComponent<BoxCollider>())
{
Debug.Log("Picked up Safe Room Key");
}
}
void batteryTextController()
{
if (battery.GetComponent<BoxCollider>())
{
Debug.Log("Picked up a Battery");
}
}
So do you have a trigger collider with this script attached to it, and when the player enters it, the player should pick up the saferoomkey and the battery?
Or is this script attached to both saferoomkey and battery and you want the player to pick up whichever he collides with ?
The later, What I want is I have lets say 3 different objects all with box collider triggers, they all have this same script attached to it. When the player enters any of their colliders I want it to run this scripts and search what collider the player entered so it can display different text for each collider.
Answer by murkertrer · May 15, 2016 at 04:24 PM
void OnTriggerEnter(Collider col)
{
if (col.GetComponent<SCRIPT NAME>())
{
//have this script
}
else
{
//dosent have it
}
}
Sorry, That does not appear to be what I need. I had exactly that become my current script where I combined all the text scripts into one. Let me try to clear up what I am trying to do, I have 3 different item in my game. They all have scripts "itemTextController" attached to it. Also they have a box collider attached to them. When the "Player" enter's the box collider of "ANY" of these items I want it to run this scripts and search out the right box collider.
That is the reason I reference all the game objects so I could call their box collider's but I cant seem to do that for some reason.
I even tried
coll.GetComponent < itemTextController >() && safeRoom$$anonymous$$ey
Thank You for your response though.
Hey sore for taking some time..
Why dont you put a variable in each object your want to identify.
Something like-
public int numberForIdentification;
and modify in the inspector.
Just check if the variable is present when checking for the collider.`
if (col.GetComponent<SCRIPT NA$$anonymous$$E>()).numberForIdentification == 1)
{
//this is object one.
}
`
Answer by NoseKills · May 17, 2016 at 10:04 PM
So in short you have a player and a number of objects with a scrip. When the player collides with any object, you want to print out its name.
The thing you are missing here is that this script is attached to all the objects (as you said) and the script "knows what object it is attached to". When you collide with the objects, each object (that collides) runs its own OnTriggerEnter. That method doesn't need to check all other objects, just itself. Something like
private GameObject player;
void Awake()
{
player = GameObject.Find("Player");
}
void OnTriggerEnter(Collider coll)
{
// something collided with the object that
//this script is attached to
if (coll.gameObject == player)
{
// it was the player this object collided with
Debug.Log("Picked up " + name);
// print the name of this object
}
}
Also, the basic mistake in your if-checks is that you are not comparing what is colliding with what.
//this just checks whether battery has a BoxCollider
if (battery.GetComponent<BoxCollider>())
In order to know if it's the right collider, you must compare it with something
void OnTriggerEnter(Collider coll)
{
if (coll == battery.GetComponent<BoxCollider>())