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 Damo1175 · Sep 27, 2016 at 02:13 PM · c#objectsgameobjects

C# Accessing another objects script values

Hi, I'm new to using Unity. So far I've managed to learn quite a lot, but I've come across a problem, which i've attempted to look for solutions although none seem to work for me.

I have three objects. The player, a door, and a collision object. What i'm aiming for is, when a player walks over the collision object, the door will close.

Names of my objects: 1. Player = Player 2. Collision object = colision 3. Door = frontdoor

My door also has a prefab which is called innerdoor which will be the object with an animation to open. This innerdoor object has been referenced by frontdoor by a script value. The value is called "doorInside".

I've tried the following

 Frontdoor = GameObject.Find ("frontdoor");
 Innerdoor = GameObject.Find (Frontdoor.doorInside);
 
 Frontdoor.doorInside.GetComponent<Animation>().Play("Close");

So Frontdoor is the name of the whole door object. doorInside is a value from a script which references the actual object that the animation is on which is called innerdoor

Sorry if this has been a terrible explanation, hopefully someone can help though.

Thankyou

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OctoMan · Sep 27, 2016 at 02:31 PM

First make sure your player has a rigidbody component & collider.

Also show us the complete copy of your OnTriggerEnter() function which might be on your trigger of the door.

Comment
Add comment · Show 9 · 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 Damo1175 · Sep 27, 2016 at 04:08 PM 0
Share

The OnTriggerEnter works fine, its just I'm unsure how to access another objects scripts

avatar image OctoMan Damo1175 · Sep 27, 2016 at 04:45 PM 0
Share

This got asked i bet 100mio + times. You need to get the component of whatever you hit for example:

 if(other.tag == "door"")
 {
 other.gameObject.GetComponent<DoorScript>().valueToChange = iDont$$anonymous$$now.
 }

$$anonymous$$ake sure the values you want to access are public. Same for functions and so on.

avatar image Damo1175 OctoMan · Sep 28, 2016 at 09:17 AM 0
Share

I'm having troubles with the GetComponent, because the script i'm trying to access which is on a different object keeps on co$$anonymous$$g up saying the type or namespace cannot be found.

I'm using this:

 objectname = GameObject.Find ("Frontdoor");
 objectname.GetComponent<DynamicObject>().locked = true;

Here is an image of what Frontdoor looks like. So what i'm pretty much trying to do is change that Locked to true by a script that is running on a different object alt text

1.png (31.9 kB)
Show more comments
avatar image OctoMan Damo1175 · Sep 28, 2016 at 09:34 AM 0
Share

Please post your whole OnTriggerEnter Function, otherwise i can't help. It seems you do something i don't see.

avatar image Damo1175 OctoMan · Sep 28, 2016 at 09:52 AM 0
Share

This is my whole OnTriggerEnter function

 void OnTriggerEnter(Collider col)
     {    
         if (col.tag == "Player")  
         {
             objectname = GameObject.Find ("Frontdoor");
             objectname.gameObject.GetComponent<DynamicObject>().locked = true;
 
             
             currentobject = GameObject.Find ("closeFrontdoor");
             Destroy(currentobject);
         }
     }
Show more comments

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

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to correctly count number of objects? 2 Answers

Call function of a MonoBehaviour class in another class 0 Answers

Add object of specific width to a List 0 Answers

Unity MonoBehaviour Objects vs Game Logic 1 Answer

See if function returns a certain gameObject 0 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