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 GiViZed · Sep 07, 2016 at 06:44 PM · c#unity 5

Accessing variable from another gameobject does not work

I`ve tryed different methods, but none of them worked for some reason. What am i doing wrong?

Script 1:

 public class FloorProtector : MonoBehaviour {            
 GameObject temp = GameObject.Find("Cube");
 void Start() {        
     BoxBehavior script = temp.GetComponent<BoxBehavior>();        
     script.isColliding = true;//testing if it works or not
 }

...

Script 2:

 public class BoxBehavior : MonoBehaviour
 {    
 public bool isColliding = false;

...

Comment
Add comment · Show 6
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 Bonfire-Boy · Sep 07, 2016 at 08:21 PM 1
Share

It's not very helpful to just say "it doesn't work". Tell us what does happen.

avatar image GiViZed Bonfire-Boy · Sep 08, 2016 at 11:05 AM 0
Share

It does not change isColliding value

avatar image Bonfire-Boy GiViZed · Sep 08, 2016 at 11:12 AM 0
Share

If the line script.isColliding = true; (in FloorProtector.Start()) runs without throwing a NullReferenceException then it does set that value.

But maybe not on the component that you intend it to.

Or, if it is the right component, maybe something else is changing it back before you inspect it.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Lewwwer · Sep 07, 2016 at 06:59 PM

I think the problem is that you tried to find the Cube in the variables. You shoud allways use the GameObject.Find() in the Start() and in the Update()

so the code is:

 public class FloorProtector : MonoBehaviour {            
  GameObject temp;
  void Start() {        
      temp = GameObject.Find("Cube");
      BoxBehavior script = temp.GetComponent<BoxBehavior>();        
      script.isColliding = true;//testing if it works or not
  }
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 GiViZed · Sep 07, 2016 at 08:06 PM 0
Share

Thanks for your reply, but it didn't help for some reason, maybe my unity editor is acting weird again

avatar image Bonfire-Boy GiViZed · Sep 07, 2016 at 08:24 PM 0
Share

Tell us what happened when you tried it.

Lewwer's code should work IF

1) you have a FloorProtector component on an active GameObject and 2) you have an active GameObject called "Cube" which has a BoxBehaviour component at the time when the FloorProtector's Start() function is called.

If those conditions are met and it's still not working, try adding some logging (first of all, to verify that that Start function is getting called).

Whatever's going on here, it's not to do with accessing another component's variables (your script.isColliding = true is fine).

avatar image GiViZed Bonfire-Boy · Sep 07, 2016 at 09:26 PM 0
Share

Here is the first script http://pastebin.com/Eu6rL9HS here is the second: http://pastebin.com/Yd6imW9J I am trying to change the variable "isColliding" in the second script so that either first or the second if statement is executed. But right now it doesn`t change, Debug.Log(isColliding) in the second script shows false when i shoot a box, which is colliding with the GameFloor (wich has the FloorProtector script attached to it). Sorry for poor english)

avatar image
0

Answer by nenok1 · Sep 08, 2016 at 12:49 PM

Try to make refference in the Editor:

  public class FloorProtector : MonoBehaviour {     
     [SerializeField]
     BoxBehavior script;        
     void Start() {             
       script.isColliding = true;
     }
   }

If you really want to use Find, make shure that:

  • there is only 1 GameObject named "Cube" (because Find will return first match);

  • BoxBehavior is on the "Cube" (not its child or parent).

Comment
Add comment · Show 8 · 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 GiViZed · Sep 08, 2016 at 01:01 PM 0
Share

I have more then 1 cube in the scene. This code also doesn`t work(

avatar image nenok1 GiViZed · Sep 08, 2016 at 02:04 PM 0
Share

Does it throw "Null refference" or "$$anonymous$$issing refference" exception? Anyway it seems like BoxBehavior (or another guy) is changing value back. Try to log all the changes and see their source:

 public class BoxBehavior : $$anonymous$$onoBehaviour { 
     private bool _isColliding = false;
     public bool isColliding
     {
         get { return _isColliding; }
         set
         {
             Debug.Log("isColliding is being changed: from " 
                 + _isColliding 
                 + " to " 
                 + value);
             _isColliding = value;
         }
     } 
 }

Here is how it works: https://yadi.sk/i/cXxbR3qiuu$$anonymous$$jn

avatar image GiViZed nenok1 · Sep 08, 2016 at 04:29 PM 0
Share

Here are 2 screenshots from my unity editor https://yadi.sk/d/5Qj9A-CJuubkg (is the link accessible?) i tried putting this code into my BoxBehavior script like that http://pastebin.com/yNye5Xi6 but it didn`t change anything. I`m feeling bad for taking your time, thanks a lot for your support. I`m a noob right now thats why i`m having so much trouble with simple things

avatar image Bonfire-Boy GiViZed · Sep 08, 2016 at 02:09 PM 0
Share

So what are wanting to achieve with the Find() call, given that you have more than 1 Cube object? And when you say that isColliding isn't changing, have you checked all your Cube objects?

avatar image GiViZed Bonfire-Boy · Sep 08, 2016 at 04:31 PM 0
Share

I thought that Find() will grab the script from any object and it will be fine

Show more comments
Show more comments
avatar image GiViZed · Sep 08, 2016 at 07:56 PM 0
Share

Ive tried this code again, in BoxBehavior ive put Debug.Log(isColliding) in the Update function it doesn`t change, it is still false, i don`t change it in any other script. It is just "public bool isColliding = false;" in one script and
[SerializeField] BoxBehavior script;
void Start() {
script.isColliding = true; }

Ive even deleted everything from FloorProtector script except for this code, just to change the bool value but it just doesnt work i have no clue why

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

7 People are following this question.

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

Related Questions

How to interrupt wait coroutine in a loop? 0 Answers

Let the Camera Face the middle of the Map while still following the Player? 1 Answer

Flip 3D Character Rotation 180 on Y axis 1 Answer

How to fix snake behavior in Unity3d? 0 Answers

How can I make this character move smoothly? 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