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 Danteeva · Jan 16, 2014 at 03:36 PM · c#movementvariabletargetingaccessing

Accessing a variable from another script on the same object? C#

Hello again. For my game I am trying to get my clickToMove script to access my targeting script so instead of when you right click an enemy and it targets and then continues to walk to the location that you click, instead it takes the target from the other script and walks in front of the target instead. The problem is I don't know how to access the variable on the other script.

The game component is a sprite.

I have checked quite a few different answers but none of them seem to work or apply to what I'm trying to do.

Here is the target select bit:

 private Transform selectedTarget = null;
 
 void Update(){
     if (Input.GetMouseButtonDown(1)){ // when button clicked...
         Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         //Debug.Log (pos);
         RaycastHit2D hit; // cast a ray from mouse pointer:
         hit = Physics2D.Raycast (pos, Vector3.zero);
         
         if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
             DeselectTarget(); // deselect previous target (if any)...
             selectedTarget = hit.transform; // set the new one...
             SelectTarget(); // and select it
         }
     }
 }
Comment
Add comment · Show 1
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 Sundar · Jan 16, 2014 at 04:12 PM 0
Share

There may be more than one ways to do this, if I were you this is how I will do it 1. Have a gameobject parameter for SelectTarget method 2. Pass gameobject hit 3. Use GetComponent to get target's script and it's variable.

Like this

        if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
          DeselectTarget(); // deselect previous target (if any)...
          selectedTarget = hit.transform; // set the new one...
          SelectTarget( hit.transform.gameObject); // and select it
 
         }
 
 
         void SelectTarget( GameObject target )
         {
               variableType variableFromTarget = target.GetComponent<targetScript>().theVariable;
              .
              .
              .
         }

1 Reply

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

Answer by Owen-Reynolds · Jan 16, 2014 at 08:01 PM

You look at one of your other scripts the same way you look at a script on someone else, using getComponent.

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html is the official reference man page. Sundar (comment above) has an example in SelectTarget.

Since the place to look is "you," you can just write GetComponent with nothing in front of it. Or transform.Get... or gameObject.Get... if you prefer the "never leave it blank" style.

Comment
Add comment · Show 2 · 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 Danteeva · Jan 16, 2014 at 09:33 PM 0
Share

Ok so basically I need to bull the transforms position into the movement script. So would I add the position variable into the SelectTarget Void and the type this,

         OtherScript otherScript = GetComponent<OtherScript>();
         otherScript.DoSomething();

Into the update part of my movement script so i can access it? Something like:

         TargetScript targeting = GetComponent<targeting>();
         targeting.SelectTarget();

?

avatar image Danteeva · Jan 17, 2014 at 04:10 PM 0
Share

Sorry was being stupid, I've got it now, thank you :)

         Targeting targeting = GetComponent<targeting>();
         targeting.SelectTarget();

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Character doesn't stop and sits on top of target. 1 Answer

Multiple Cars not working 1 Answer

Trigger player movement on touch 1 Answer

Distribute terrain in zones 3 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