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 /
This question was closed Apr 09, 2016 at 01:54 PM by meat5000 for the following reason:

Duplicate/SUPER COMMON Question

https://www.google.co.uk/search?q=unit+object+access+variable&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=cAkJV8OYCMKFaL_GsJAC&gws_rd=ssl#channel=fs&q=unit+other+object+access+variable

avatar image
0
Question by Makarew · Apr 09, 2016 at 06:23 AM · gameobjectvariables

Access another object's variable

I'm trying to access a specific variable in a script attached to another gameobject. I have no idea how to do this. Does anyone else know? using UnityEngine; using System.Collections;

 public class StandardAttacksP1 : MonoBehaviour {
 
     public GameObject controllerDetect;
     public Component controllers;
 
     void Start () {
         controllers = controllerDetect.GetComponentInChildren<ControllerAmountDet> ();
     }
 
     void Update () {
         if (controllers.playerOneKeyboard == true) {
             //Do something
         }
     }
 }
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 Happy-Zomby · Apr 09, 2016 at 07:22 AM 0
Share

Do you get an error with this script? You are looking for a script in a child of the object holding this script correct? The child needs to be active for this to work

avatar image Makarew Happy-Zomby · Apr 09, 2016 at 01:52 PM 0
Share

The problem was that controllers.playerOne$$anonymous$$eyboard == true couldn't be used, and yes, it is in a child.

avatar image meat5000 ♦ · Apr 09, 2016 at 01:56 PM 0
Share

This is a super common question and the answers are so rife that its amazing it they dont jump out at you.

In future, please conduct searches for existing material before posting a question which is actually answered in the manual.

http://docs.unity3d.com/410/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by JigneshKoradiya · Apr 09, 2016 at 07:30 AM

GameObject.Find("objectname").GetComponent().varibalename = 2;

make sure that variable are public

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 Makarew · Apr 09, 2016 at 01:53 PM 0
Share

Thanks. This worked wonderfully. All I needed to change was the = 2 part.

avatar image JigneshKoradiya Makarew · Apr 09, 2016 at 02:05 PM 0
Share

welcome ...!! :)

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 to get scripts to detect foreign variable change? 0 Answers

destory object not variable 2 Answers

Novice Question: Assigning GameObject properties efficiently. 1 Answer

How to get access to variable from another script without knowing script's name c# 2 Answers

storing velocity and temporarly using it 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