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 /
avatar image
0
Question by carlqwe · Apr 12, 2016 at 02:28 PM · javascriptmovementgameobject

Make so the script only moves and makes actions to the gameObject that its attached to.

Hello having a major problem, i've developed a script that can select, deselect and move a worker or hero. But if i spawn more than one in the scene then all the different workers will do the same order, even if THEY are not told to. Is there a way of fixing this? Thanks :)

here is my worker Controller

(worker and hero is the same thing) Dont think you will need the move to mousecursor position,

 #pragma strict
 
 var SelectedRing : GameObject;
 var Selected : boolean = false;
 
 var showGUI : boolean = false;
 
 var WorkerSpawn : GameObject;
 var Hero : GameObject;
 
 var lastHit: GameObject; // Keep track of last object clicked
 
 
 
 function Update ()
  {
   //Selects Worker && Deselects worker
   var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   var hit : RaycastHit;
   
       if(Input.GetMouseButtonDown(0))
       {
          if (Physics.Raycast(ray,hit) && hit.transform.gameObject == Hero)
          {
                    Selected = true;
               SelectedRing.SetActive(true);
          }
           else
          {
               Selected = false;
               SelectedRing.SetActive(false);
          }
       }
       
       //ShortKey To spawn other worker
       if(Input.GetKeyDown(KeyCode.Q))
       {
           showGUI = true;
       }
       
       //Instant Stop Any Movement
       if(Input.GetKeyDown(KeyCode.Space))
       {
           Hero.transform.position = transform.localPosition;
       }
  }
 
 function Start ()
 {
     SelectedRing.SetActive(false);
 }

Comment
Add comment · Show 4
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 tanoshimi · Apr 12, 2016 at 02:35 PM 0
Share

And where do you assign Hero?

avatar image carlqwe tanoshimi · Apr 12, 2016 at 03:24 PM 0
Share

I need it when i detect it with the raycast

avatar image tanoshimi carlqwe · Apr 12, 2016 at 03:34 PM 0
Share

No - you're testing it there. But where do you assign it? Otherwise it's always null.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by coolraiman · Apr 12, 2016 at 03:37 PM

is this script attached to each worker/hero?

also can you show the code where you give an order?

also for optimization you should put var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit;

inside the bracket of that condition if(Input.GetMouseButtonDown(0))

so you only calculate the raycasting if there is a click

Comment
Add comment · Show 1 · 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 carlqwe · Apr 13, 2016 at 07:36 AM 0
Share

yes this script is attached to every single worker

avatar image
0

Answer by giorashc · Apr 13, 2016 at 08:02 AM

Do not attach the script to each Hero/Worker as it will cause the game click test (and raycast) to be executed for each Hero/Worker (e.g. if you have 100 heroes the raycast/click test will be executed 100 times per update! which will be inefficient).

Instead attach the controller script to a single game object and use a tag to detect which object has been hit:

 if (Physics.Raycast(ray,hit) && (hit.transform.gameObject.CompareTag("Hero") || hit.transform.gameObject.CompareTag("Worker") ) 

EDIT: If you want the ring to appear on each clicked hero/worker then attach the ring object under the hero/worker game object (in the hierarchy) and disable it by default. In the hero/worker script declare a public member to hold a reference to a ring prefab and when a hero/worker is selected extract the selected game object and enable the ring game object:

 if (Physics.Raycast(ray,hit) && (hit.transform.gameObject.CompareTag("Hero") || hit.transform.gameObject.CompareTag("Worker") )  {
      HeroScript heroScript = hit.transform.gameObject.GetComponent<HeroScript>();
      heroScript.ring.SetActive(true);
 }



Comment
Add comment · Show 7 · 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 carlqwe · Apr 13, 2016 at 12:04 PM 0
Share

okay so im going to attach the script to maybe the main camera, and ins$$anonymous$$d of if (Physics.Raycast(ray,hit) && hit.transform.gameObject == Hero) {

im going to add that pice of code that you wrote? appreciate it :)

avatar image carlqwe · Apr 13, 2016 at 12:39 PM 0
Share

The problem is i have a lot of the "workers" and i need to attach a selected ring to the maincamera where this script is attached. But when i do so i can only attach the select ring from ONE worker, and not everyone. If you dont understand what im going for, it's something like in a RTS (real time strategy) or like in Age Of Empires 2

avatar image giorashc carlqwe · Apr 13, 2016 at 12:58 PM 0
Share

Do you mean if you click on a hero/worker you want to show a ring under it? Like a multi select of heroes/workers?

avatar image carlqwe giorashc · Apr 13, 2016 at 01:11 PM 0
Share

exactly thats why i kinda showed you the script, so you could understand better.

Show more comments
Show more comments
avatar image giorashc carlqwe · Apr 13, 2016 at 02:13 PM 0
Share

The idea I'm trying to say is that keep the ring as part of the hero/worker game object (i.e. in its hierarcty and as a reference in the hero/work gameobject script) and just activate/disable it when selected/unselected. You can access this object via the gameObject you get in the hit object from the raycast. You can remove the SelectedRing reference from the camera script as it now will be per hero/worker gameobject due to the explanation above.

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

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

Related Questions

Enemy circles player instead of attacking. 1 Answer

Interaction around an gameObject 1 Answer

How to move a Game Object from a script not attached to it. 1 Answer

static var for one object. 2 Answers

"GameObject.Find();" not working 2 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