- Home /
Clicking a sprite to enable script
In unity 2D i have the scripts for moving, and everything else but i want it to not enable controls for that object unless i click it and having AI that wanders but you can click them to control them
Update: first i tried it it just said "expecting (, 'OnMouseDown' and if its C# it would say parsing error
also what im trying to say is if there is a character he would be moving around but if i click on him it will disable moving then turn on playercontrols. also i only have player controls not AI or anything (this is my first time using Unity)
Am not sure your question is detailed enough. I decided to not reject it, but think you might want to add a bit more information about what you've tried.
As Graham Dunnett mentioned the question is a bit vague, however bobin115 is on the right track. The only change I would make is that I would not disable the entire script as you mentioned "wandering" behaviour when the player is not controlling the object. Ins$$anonymous$$d use a boolean to deter$$anonymous$$e if the player is controlling the script or your AI is. When clicked change this bool.
Answer by InsertFunnyUsernameHere · May 14, 2015 at 04:56 PM
Agreed with bobin115, you should probably try OnMouseDown() function, which is called everytime a GameObject is clicked
Answer by bobin115 · May 14, 2015 at 03:56 PM
this i a code i quickly wrote (untested). basically when you click the object with the script on it will enable your selected script.
var script : thescript;
function Start() { script = GetComponent(thescript); script.enabled = false;
function OnMouseDown() {
script.enabled = true;
}
hope this helps