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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by cheadleand · Sep 09, 2014 at 12:57 PM · c#gameobjectgetcomponent

Accessing a value in a script attached to another game object

I am trying to access a variable stored in a script attached to another game object.

I seam to be able to access the script with this line Component theScript = OBJhit.GetComponent(opponentScript); However, when I try to change the value stored in that script (value) it turns red and I get the following error - Expression denotes a type where variable value or method group was expected.

Could anyone point me in the right direction?

 if (Physics.Raycast (transform.position, fwd, out hit, 20)) {
     OBJhit = hit.transform.gameObject;
         if (OBJhit.tag == "opponent") {
             Component theScript = OBJhit.GetComponent(opponentScript);
             theScript.value = true;
         }
 }
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

3 Replies

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

Answer by jokim · Sep 09, 2014 at 01:13 PM

HarshadK had part of the answer right, but I am not sure the way you're using GetComponent is the right one. (Not sure because I believe there's 2 ways to use it)

The way I recommend using it is as a template function, here's how to call it :

 opponentScript theScript  = OBJhit.GetComponent<opponentScript>();
 theScript.value = true;



Edit : After double checking, HarshadK had a working answer... I would still try this way and see if the result is any different.


re-Edit : third answer has me corrected : GetComponent("opponentScript") is the way to call it. (other than mine)

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 HarshadK · Sep 09, 2014 at 01:15 PM 1
Share

+1. This will generate a compile time error if the component is not found.

avatar image cheadleand · Sep 09, 2014 at 01:16 PM 0
Share

Perfect! Thanks

avatar image
1

Answer by HarshadK · Sep 09, 2014 at 12:58 PM

Make your theScript variable of type of your script as below:

 opponentScript theScript  = OBJhit.GetComponent(opponentScript);
 theScript.value = true;

Edit: Actually it should have been

 opponentScript theScript  = OBJhit.GetComponent("opponentScript");
Comment
Add comment · Show 4 · 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 cheadleand · Sep 09, 2014 at 01:08 PM 0
Share

Hi, I have done that, however, "theScript" on the next line is highlighted in red (im working in c# if that makes any difference)

avatar image HarshadK · Sep 09, 2014 at 01:14 PM 0
Share

Is your value variable a bool?

Judging by the name it looks to be an int or float.

avatar image cheadleand · Sep 09, 2014 at 01:17 PM 0
Share

its a bool

avatar image HarshadK · Sep 09, 2014 at 01:19 PM 0
Share

Then I would just suggest to use proper names for your variables. It's a part of good coding practice.

avatar image
1

Answer by bhartu · Sep 09, 2014 at 01:16 PM

GetComponent() has three overloaded methods.

if you are using GetComponent("string"), then you can write OBJhit.GetComponent("opponentScript");

or if you are using GetComponent(System.Type) then you have to write OBJhit.GetComponent<>();

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

insert script question 1 Answer

C# GetComponent Issue 2 Answers

How to get a variable value from another script(C#)? 1 Answer

C# Preserving GameObjects' Previous Meshes 1 Answer

"Center On Children" programmatically 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