- Home /
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);
}
No - you're testing it there. But where do you assign it? Otherwise it's always null.
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
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);
}
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 :)
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
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?
exactly thats why i kinda showed you the script, so you could understand better.
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
Follow this Question
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