Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Angel-Regrets · May 15, 2016 at 04:04 PM · gameobjectreferenceif-statementsboxcollider

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");
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · May 15, 2016 at 04:16 PM 0
Share

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 ?

avatar image Angel-Regrets NoseKills · May 15, 2016 at 08:42 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
 }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Angel-Regrets · May 15, 2016 at 08:52 PM 0
Share

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.

avatar image murkertrer Angel-Regrets · May 17, 2016 at 04:01 PM 0
Share

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.
 }

`

avatar image
0

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>())
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get an object reference from a Raycast? 1 Answer

Object reference; sometimes works but sometimes doesn't??? 2 Answers

how do I add link script to gameobject that already exist with attached script 1 Answer

How do I get my weapon to stop firing if the ammo <= 0 1 Answer

Access gameobject from a different scene... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges