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 Sakuri · Sep 23, 2014 at 03:09 AM · getcomponentmonobehaviourboolotherscript

Writing a bool to a separate script without scripts name?

Hello.

At the moment, I am currently trying to change a bool to true from simply finding the monobehaviour that the bool is located in. I am able to find the component, (each monobehaviour has a separate name, but has the same bool, that I need to change with a ray), but the engine does not recognize the bool. It gives me this error: "Type Component does not contain a definition for [bool name] and no extension method [name of the bool] of type 'Component' could be found. Are you missing using a directive or an assembly reference?"

The problem is that, until I can show it, in the game, what object the script is in, I am unable to point it to it, thus, it can't find the name of the bool, and I can't even run the game.

Is there any way of having it let me bypass that, by checking first in the code some how if it has the bool within the monobehaviour, so then it will understand that I will not have the script with the bool until it is time?

(Basically, I hover the mouse over an object, to get the code. And each object has a different code, so I can't specify it by name, but need to find the bool with code.)

Thank you in advance for any assistance or help! I am sorry. Thanks for reading!

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
0
Best Answer

Answer by Sakuri · Sep 23, 2014 at 03:09 PM

Alright. I've managed to solve the issue! Thank you to you two that answered. And for that link! That helped me in working towards solving the issue ^-^

I solved the issue, by simply using Component.SendMessage()

EDIT: This is C#.

I'll give an example, in case someone from the future stumbles upon my question:

 //In the script that contains the code that >finds< the MonoBehaviour component that we do not yet know the name of, can be used with the SendMessage function like so:
 
 
 private GameObject exampleObject;
 
 void ExemplaryUpdate()
 {
       exampleObject = (code that finds the game object that contains the component)
       
       exampleObject.GetComponent<MonoBehaviour>().SendMessage("NamelessCodeFunction", true);
 }
 
 //In the "nameless" script that we are sending a value to:
 
 
 private bool namelessScriptBool = false;
 
 
 void NamelessCodeFunction(bool sakuriBool)
 {
       namelessScriptBool = sakuriBool;
 }


I didn't test this after writing it, of course. Though, I hope I could help someone by finding a solution! Thanks again for the help you two ^-^

Have a good day, everyone.

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 robertbu · Sep 23, 2014 at 03:17 AM

Given multiple scripts each with the same boolean, see @asafsitner's answer to this question:

http://answers.unity3d.com/questions/381529/how-to-address-scripts-without-knowing-their-names.html

I've referenced this question a number of times for various questions. Even though he states that overriding a base class is, "which is arguably worse practice than the previous options," it is the solution which most seem to use.

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 Kiwasi · Sep 23, 2014 at 03:21 AM

You have a couple of options. The first is to implement a common interface on all of your components. This will let you access the component as the type of the interface instead of needing to know its exact type.

The second is to use reflection. This lets you access members without knowing any of the details in advance. In most cases reflection can be avoided with a decent structure.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I check a bool from another script?? 2 Answers

how to use bool from another script in if statment? 2 Answers

Trouble getting a bool from player script for camera script 2 Answers

bool field initialization ignored 0 Answers

Can't Call function in another script 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