I have an animated sprite I want to execute only when I click a specific object, let’s call it object X. I’ve got a script that works, but it works regardless of where I click as long as I click the mouse button anywhere within the game screen.
How do I specify that it should only execute if I press object X.
Here’s my code :
public class SpriteActivate1 : MonoBehaviour {
public GameObject SpriteController;
void start () {
SpriteController.active = false;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
SpriteController.active = true;
}
}
This script is on object X and allows me to drag and drop my animated sprite onto “SpriteController”. But like I said before it doesn’t matter what this script is on or where I click, it executes/activates the animated sprite whenever I click the mouse button anywhere. Can anyone tell me how to specify that this script should only work when I click object X? Thanks
you can simply check it with the “name” or “tag” or “components”
//Do this in the FixedUpdate because you are using Physics
void FixedUpdate ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if(hit.gameObject.name == "ball");
if(hit.gameObject.tag == "ball");
if(hit.gameObject.getComponent<BallScript>() != false);
}
}
}
public class SpriteActivate : MonoBehaviour {
public GameObject SpriteController;
void start () {
SpriteController.active = false;
}
// Update is called once per frame
void FixedUpdate ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if(hit.gameObject.name == "rotator");
SpriteController.active = true;
}
}
}
}
I apologize in advance if this seems stupid but when I put this script on the object called “rotator”, which I called object X before, then it says unity Raycast hit does not have a definition for game object. and in the line "if(hit.gameObject.name == “rotator”); " gameObject turns red. How do I define gameObject as the object that is called “rotator” in this case?
You need to Drag this Script onto you Camera, because the Raycast uses the OnScreenMousePosition.
Also check if your “rotator” Object has got an Collider (do not Trigger that Collider)
when you have finished that change the Script like that:
Alright, I dragged this script onto the camera and the “rotator” Object has a Collider that isn’t set as trigger (Mesh Collider). And I changed the last part to what you wrote but it still says gameObject is not defined and its red. Do i have to write something about the SpriteController object before void Start? I really appreciate your help Im new to this and trying to learn so thanks a lot for your help
better you make an image that i can see whats your error
also make shure your gameobject is named “rotator” !
the next thing is…SpriteController… is that a script you have written… if it is a costum script from you attach that to your rotator Gameobject, next you could look is (aktive) is that a variable in your spritecontroller if it is make shure you have set the variable to public
if you mean (aktive)to enable or diable your script write that:
Just use OnMouseDown. No need for manually raycasting and so on. (Also, using GetMouseButtonDown in FixedUpdate does not work and should never be used, since *Down and *Up events are only true for the single frame in which they occur, which may or may not coincide with a physics frame.)
I got the fix. I know i must have confused you with “SpriteController” calling it an object before. SpiteController was just a public float to be able to drag and drop my animated sprite onto the “rotator” in the inspector. Thanks for your help though I’ve learned a little more about Raycasting #LongWayToGoThough