• 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 Glarmo · Nov 16, 2014 at 04:26 AM · transforms

How can I give another script my transform.

I have two scripts, target and movement. I want to give movement the transform of the gameObject that target is attached to. I can't do this through the inspector because there is multiple of both objects and so I don't think that's possible. What I'm doing currently is using getComponent to access movement and there changing the transform variable it has to the transform of the gameObject.

 void Start ()
     {
         target.position = transform.position;        
         script = gameObject.GetComponent<movement> ();        
     }

This is from the target script and I'm getting the transform of the attached gameObject and then getting access to the movement script.

 void OnMouseOver ()
     {
         if (Input.GetMouseButtonDown (0)) 
         {
             Vector3 spawnPosition = new Vector3 (0,0,0);
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (policeCar, spawnPosition, spawnRotation);
             script.destination = target;
         }
     }

The last line is where I access the variable target in the movement script and set it as target.

     public Transform destination;
     private NavMeshAgent navComp;
     
     void Start () 
     {
         navComp = GetComponent <NavMeshAgent>();
     }
 
     void Update () 
     {
         navComp.SetDestination(destination.position);
     }

Finally, here is the simple movement script. So far I haven't been able to make this work, so I was wondering if there was a simpler way of giving another script the transform?

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 Glarmo · Nov 16, 2014 at 11:56 AM 0
Share

bump......

avatar image richyrich · Nov 16, 2014 at 12:37 PM 0
Share

I don't believe your question is so complex that others could not contribute to it. The problem is that it is not clear exactly how to help.

This is from the target script and I'm getting the transform of the attached gameObject and then getting access to the movement script.

Ok

The last line is where I access the variable target in the movement script and set it as targetPosition

There seems to be targetPosition and script object in both scripts - this is very confusing for other community members to help.

targetPosition - guessing this is a Transform?

 navComp.SetDestination(target.position);

Setting a Vector3, not a Transform - confused. I don't know much about Nav$$anonymous$$esh systems, but if you could clear up the other queries, then it might give someone else a better chance of helping

Note: I realise that SetDestination takes a Vector3

1 Reply

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

Answer by Scribe · Nov 16, 2014 at 01:47 PM

 script = gameObject.GetComponent<movement> (); 

the movement script and the target script are attached to the same object? if so why not just use the transform from movemnt to stat with and if not then gameObject should be a variable, not gameObject

Comment
Add comment · Show 6 · 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 Glarmo · Nov 16, 2014 at 01:53 PM 0
Share

The movement script is attached to a different object but I'm not sure I understand. I'm trying to get the component of another gameObject so it should be gameObject.getComponent right?

avatar image Glarmo · Nov 16, 2014 at 01:58 PM 0
Share

I have edited the script to, I hope, make it more clear with the names (sorry). Target, formerly targetPosition, was a transform. It was the transform of the gameObject that the script was attached to. Also destination, formerly target, is a transform I declare it as a transform and set it as the transform in the target script. Sorry about the names I should've noticed earlier that it was quite unclear. I'm still uncertain whether or not it is clear. Thanks

avatar image Scribe · Nov 16, 2014 at 01:58 PM 0
Share

no, you need a GameObject variable to get the component on a different gameobject:

 GameObject some$$anonymous$$ovementController; //assign this in the inspector or use a gameobject.find method in start
 
 void Start(){
     script = some$$anonymous$$ovementController.GetComponent<movement>();
     //get the movement script attached to some$$anonymous$$ovementController
 }
avatar image Glarmo · Nov 16, 2014 at 02:01 PM 0
Share

Okay I will try this, thanks

avatar image Glarmo · Nov 16, 2014 at 02:13 PM 0
Share

hahahaha, it works, sort of.. There are some very weird errors but the core of it works. Thanks mate you helped me alot :)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I scale a 2nd level child in transform hierachy. 2 Answers

[Solved] Problem Finding Closest GameObject to Cursor Point 1 Answer

geometric skybox camera? 1 Answer

Platform Triggers & Animation 0 Answers

Iterating a GO through an array of transform positions 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