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 cj31387 · Sep 27, 2012 at 08:30 AM · variablegetcomponent

Variable from a external script not being effected

I have script A (ChildCollision) which is a collision script that I put on my child objects of my prefab. In that script I tell a variable bool isEmptySlot1 to be false when a collision happens. But in Script B (Formation_AI) where the variable is it does not change for some reason it remains true. I've put debug log on the collision script and THAT one says it turns false, but the variable in script B (Formation_AI) does not, I don't know what the problem is, basically I set the script B (Formation_AI) as a instance that is referenced in Script A (ChildCollision). Here is some of my Code.

 ChildCollison.cs
 public class ChildCollision : MonoBehaviour {
     // Access Formation AI
     public Formation_AI get;
 
 
     void Awake()
     {
         Formation_AI get = GetComponent<Formation_AI>();
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.collider.tag == "Enemy")
         {
             get.isEmptySlot1 = false;
             Debug.Log("Collided" + other);
             Debug.Log("Emptyslot" + get.isEmptySlot1);
         }
     }
 }

That is script A and the variable says false but here in the next script its not getting changed to false it stays true.

 // This right here is in Script B (Formation_AI)
 Formation_AI.cs
 public bool isEmptySlot1 = true;  // if the slot is empty it can be filled




My third script (Fighter_AI) where it is supposed to work uses that bool but its not working because its not changing Formation_AI isEmptySlot1 to false from script ChildCollisionScript

 Fighter_AI.cs
 void Update () {
         transform.LookAt(playerTarget);
         if (script.isEmptySlot1 == true)
         {           
             transform.position = Vector3.Lerp(transform.position, FormTarg1.position, Time.deltaTime * smoothTranslateSpeed);
             Debug.Log("FighterSlot" + script.isEmptySlot1);
         }
 
         else if (script.isEmptySlot1 == true)
         {
             transform.position = Vector3.Lerp(transform.position, FormTarg2.position, Time.deltaTime * smoothTranslateSpeed);
 
         }
         else if (script.isEmptySlot1 == true)
         {
             transform.position = Vector3.Lerp(transform.position, FormTarg3.position, Time.deltaTime * smoothTranslateSpeed);
 
         }
         
         
         if (playerDist)
         {
             float dist = Vector3.Distance(playerDist.position, transform.position);
            //Debug.Log("Distance to other: " + dist);
         }
     }

The Debug.Log here says true when I clearly change it to false on collision, it stays true when its collision happens

So the MAIN QUESTION is how do you change a variable that belongs to another script from another script using GetComponent

Comment
Add comment · Show 13
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 whydoidoit · Sep 27, 2012 at 08:33 AM 0
Share

All of these scripts are attached to the same GameObject?

avatar image whydoidoit · Sep 27, 2012 at 08:35 AM 0
Share

Also your Update looks strange (you are testing the same variable in each if/else if). What is script and how did you get it?

avatar image whydoidoit · Sep 27, 2012 at 08:40 AM 1
Share

Oh dear not sure my brain quite followed that!

What I presume is happening is that you are modifying an isEmptySlot1 variable on one script but then reading it on a different one. The way you have the FormationAI get = in the Awake is hiding the actual variable - so you aren't really getting it there.

avatar image Seth-Bergman · Oct 01, 2012 at 05:30 AM 1
Share

what you should do is change the line:

   Formation_AI get = GetComponent<Formation_AI>();

to

  get = GetComponent<Formation_AI>();

BUT, this would only work if the script were on the same object.. since you say it is on the PARENT, you could ins$$anonymous$$d say:

 get = transform.parent.gameObject.GetComponent<Formation_AI>();

that should work I think

avatar image Seth-Bergman · Oct 01, 2012 at 05:34 AM 1
Share

As for if it were on a different object, you could find that object by name, or by tag, usually..

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html

once you have a reference to the GameObject (such as "hand", in the first link), you can then use that, as in:

 hand.GetComponent<whatever>...
Show more comments

1 Reply

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

Answer by Seth-Bergman · Oct 01, 2012 at 06:04 AM

(from comments)

what you should do is change the line:

 Formation_AI get = GetComponent<Formation_AI>();

to

 get = GetComponent<Formation_AI>();

BUT, this would only work if the script were on the same object.. since you say it is on the PARENT, you could instead say:

 get = transform.parent.gameObject.GetComponent<Formation_AI>();

that should work I think

As for if it were on a different object, you could find that object by name, or by tag, usually..

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html

once you have a reference to the GameObject (such as "hand", in the first link), you can then use that, as in:

 hand.GetComponent<whatever>...

(Glad I could help, good luck!)

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

12 People are following this question.

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

Related Questions

updating a gameobject variable from another script attached to another object 3 Answers

Is it possible to set variables for classes? 2 Answers

GetComponent from class 1 Answer

Issues accessing a variable in another script 0 Answers

Accessing variables in another script using GetComponent() 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