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;
}
}
}
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()?
I can't use it in start because then it says "mC does not exist in the context"
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;
}
}
}
$$anonymous$$ainControl is a script attached to the player, so I don't see how that would work?
Updated my answer. Also make sure you save the prefab with the script attached to it.
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
Follow this Question
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