Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by shane.rachel · Dec 09, 2012 at 03:33 AM · variablegetcomponentpassing

Passing variables between classes attached to different GameObjects

As I said above, I'm trying to pass a variable from one class to another, however I can't use GetComponent() the reason being that the two classes aren't attached to the same GameObject. I have no idea how to get around this, and I need this to work in order for object1 to check if object2 (instantiated by object1) is destroyed.

object1 passes a boolean true if the fire button is pressed. I want it to pass a boolean false if float x (in object2) = 0. When object2 is instantiated float x = 1;

if the bool is true, object1 is no longer functional, once false and object2 is destroyed object1 should become functional again.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jogo13 · Dec 09, 2012 at 05:11 AM

You can 'grab' another GameObject (and then use GetComponent) with: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

It's a little slow and it's considered bad practice to call every frame (It's better to use it once and save the reference.)

 GameObject reference = GameObject.Find("object2_name");
Comment
Add comment · Show 1 · 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 shane.rachel · Dec 09, 2012 at 10:07 PM 0
Share

I can just save that in void Start.

I'm seeing now that there are two GetComponent functions. One is Component.GetComponent and the other is GameObject.GetComponent. I may have been using the first one lol.

avatar image
1

Answer by landon912 · Dec 09, 2012 at 04:54 AM

You will probably not like the answer the best because it isn't what you ask for exactly, but I want you to have to edit it and therefore understand instead of copying and pasting into your game and not looking at it. Below is all the info needed to complete what you want.

How I do it(may not be the best or easiest):

 var gameobject1:GameObject;
 var gameobject1Script: Script ///Change to the name of the script you are trying to get the var from
 
 function Start(){
     gameobject1=GameObject.Find("Object1"); ///Change to name of the object the script is attached to.
     gameobject1Script = gameobject1.GetComponent(Script); ///Change to the name of the script you are trying to get the var from
 }
 
 function Update(){
 if(gameobjectScript1.dead == true)
 Debug.Log("Death");
 }
Comment
Add comment · Show 3 · 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 landon912 · Dec 09, 2012 at 04:57 AM 0
Share

Wait, I now think of that I think you want C#. since class is hidden from the user in JavaScript. Pm if you need me to translate

avatar image shane.rachel · Dec 09, 2012 at 10:09 PM 0
Share

I am using C#, but I can translate it. Sometimes it's just difficult to find the correct function to use in the script ref lol.

avatar image landon912 · Dec 10, 2012 at 12:07 AM 0
Share

mk, just so you know this also works getting var from UnityScript to a C# script

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

11 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

Related Questions

AddComponent with parameter variable 2 Answers

Passing a Script Name to a Function 2 Answers

Best way to pass variables between instantiated/colliding objects? 1 Answer

Getting Variable value from another object? 1 Answer

When passing a value it becomes null 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