• 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 /
avatar image
0
Question by JonMitten · Jun 14, 2014 at 06:14 AM · collisiongetcomponent

Get value of game object from public variable (C#)

I want my player to get the value of the value of game objects. I have, for example, a door, and the player needs to enter the correct door.

Each door has a unique value, a doorNumber int. I want the player to be required to enter a specific door.

The DoorController.cs is as follows:

 using UnityEngine;
 using System.Collections;
 
 public class DoorController : MonoBehaviour {
     public int doorNumber;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

That's it. I set the doorNumber by hand in the inspector. The Player Controller has a bit of logic for collision on doors:

     void OnTriggerEnter2D(Collider2D col) {
         if(col.gameObject.tag == "Door") {
             // doorColor = DoorColor.Red;
             onDoor = true;
             var door = col.gameObject;
             var doorNumber = door.GetComponent(doorNumber);
             while(onDoor){
                 Debug.Log(string.Format("Door Number {0}", doorNumber.ToString()));
             }
         }


I want the DoorNumber to be displayed in the debug console log when the player is colliding with the door. I store the door number on the door game-object script component, in the field that gets generated with the public variable declaration.

What am I doing wrong?

Comment
Add comment · Show 3
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 StewVanB · Jun 14, 2014 at 06:18 AM 0
Share

Before I answer, I am wondering where you have the correct door number stored?

avatar image iwaldrop · Jun 14, 2014 at 06:23 AM 0
Share

You haven't stated what goes wrong. I see a couple of things (like using a while loop in a method that doesn't return Type IEnumerator, and that the PlayerController shouldn't really know anything about how a door works), but nothing stands out as mechanically wrong.

Please update your question with the desired functionality, and that which you are currently experiencing.

avatar image JonMitten · Jun 14, 2014 at 08:53 PM 0
Share

Sorry, in an attempt to scope the question to just this particular problem, I left code in that was not supposed to be in there. I'll edit it now.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by StewVanB · Jun 14, 2014 at 06:25 AM

Place this code on the player.

         void OnTriggerEnter2D(Collider2D _other){
             if(_other.gameObject.tag == "Door"){
                 DoorController theDoor = _other.gameObject.GetComponent<DoorController>();
                 if(theDoor.doorNumber != null){
                     Debug.Log(theDoor.doorNumber);
                 }
             }
         }



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
avatar image
0

Answer by RobotRocker · Jun 14, 2014 at 06:38 AM

Hm I'm uncertain where the doorNumber at line 8 in your second section is declared and what the value 'doorFloor' means in the code:

 door.GetComponent(doorFloor); 

But getting your doorNumber should be as simple as putting this code inside the colliders if statement:

 int doorNumber = col.gameObject.GetComponent<DoorController>().doorNumber;

You can break this into separate variables for better readability, such as

 GameObject doorGameObject = col.gameObject;//Get GameObject from the collider hit
 DoorController doorController = doorGameObject.GetComponent<DoorController>();//Get the DoorController component from the doorGameObject
 int doorNumber = doorController.doorNumber;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 do you get the component/access the script of the object you hit? 1 Answer

Detecting if other GameObject collided with has a certain component 1 Answer

How to reference compound colliders? 1 Answer

How to determine if a Player collides with a particular Prefab in an array of Prefabs? 2 Answers

GetComponent on Collider not working 2 Answers


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