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 Suppapa · Sep 03, 2015 at 04:50 AM · scripting problemscripting beginnerscriptingbasics

When I change a bool in a script on one object it doesn't change the bool on the same script on another object?

So I have a script that changes a bool on and off which works but when I add the same script to another object and try to access said script with the object and it doesn't work because the boolean doesn't change for the other object (the Object has it's own script to access the boolean one, I couldn't access a script unless it was on the same object for some reason, any help with that is greatly appreciated) I'm using c# and both objects are prefabs that are instantiated.

boolean script:

 using UnityEngine;
 using System.Collections;

 public class MainControl : MonoBehaviour {
 public bool gravityAndFriction = true;
 // Use this for initialization
 void Start () {
     }
 // Update is called once per frame
 void Update () {
 
 }
 }

the script trying to access the boolean one:

 using UnityEngine;
 using System.Collections;

 public class ObjectPhysics : MonoBehaviour {
 private Rigidbody rb;
 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update () {
     MainControl mC = GetComponent<MainControl>();
     mC = GetComponent<MainControl>();
         if(mC.gravityAndFriction == false){
             rb.useGravity = false;
             rb.angularDrag = 0;
             rb.drag = 0;
     }
         if(mC.gravityAndFriction == true){
             rb.useGravity = true;
             rb.angularDrag = 0.05f;
             rb.drag = 0;
     }
 }
 }
Comment
Add comment · Show 2
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 getyour411 · Sep 03, 2015 at 04:52 AM 0
Share

You have two definitions of mC which is probably part of your GetComponent/access other script issue. Also, why do that in Update() ins$$anonymous$$d of Start()?

avatar image Suppapa · Sep 03, 2015 at 11:00 PM 0
Share

I can't use it in start because then it says "mC does not exist in the context"

1 Reply

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

Answer by Positive7 · Sep 03, 2015 at 05:54 AM

 using UnityEngine;
 using System.Collections;
 public class ObjectPhysics : MonoBehaviour {
 
     private Rigidbody rb;
     MainControl mC;
 
     void Start () {
         rb = GetComponent<Rigidbody>();
         mC = (MainControl)GameObject.FindObjectOfType(typeof(MainControl));
     }
 
     void Update () {
         if(mC.gravityAndFriction == false){
             rb.useGravity = false;
             rb.angularDrag = 0;
             rb.drag = 0;
         }
         if(mC.gravityAndFriction == true){
             rb.useGravity = true;
             rb.angularDrag = 0.05f;
             rb.drag = 0;
         }
     }
 }
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 Suppapa · Sep 03, 2015 at 11:01 PM 0
Share

$$anonymous$$ainControl is a script attached to the player, so I don't see how that would work?

avatar image Positive7 · Sep 03, 2015 at 11:11 PM 0
Share

Updated my answer. Also make sure you save the prefab with the script attached to it.

avatar image Suppapa · Sep 03, 2015 at 11:51 PM 0
Share

I now tried your answer and the script says nothing is wrong although the same problem occurs where the script doesn't change although it was changed on a different game-object, should I be looking for an object called $$anonymous$$ainControl or....?

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

Set public gameobject by raycast hit target 1 Answer

my script was written but does not want to work 1 Answer

Measuring/finding the height of a stack of game objects then recording that number. 2 Answers

Get() and Set() values of a gridArray from another scipt 0 Answers

My loop isn't working and I don't know why.,My loops arent working. 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