- Home /
Raycast return script of hit object (C#)
Hi! I'm a begginer at coding, so don't kill me if this is a stupid question and don't answer with "advanced" coding either, please. I'm trying to make my player cast a ray and, when it hits any gameobject that has a script, make it run.
For example: My character look at a door, turning its script enabled. My character look at a key, turning its script enabled and so on...
I know that it is possible to write something like: GameObject.GetComponent().enabled = true;
But I want something generic, something more like: GameObject.Scripts.enabled = true; (I know that this doesn't lol)
Is there any basic solution? Any suggestion? Thanks for the attention.
You'll probably regret this later, but you can adapt something like this
$$anonymous$$onoBehaviour[] scripts = gameObject.GetComponents<$$anonymous$$onoBehaviour>();
foreach($$anonymous$$onoBehaviour script in scripts)
{
script.enabled = false;
}
If you go this route, you'll probabyl want to add additional logic like if script.name = "VeryImportantOne" don't disable it, etc.
Thanks for commenting! Will try this later cuz I'm about to go out, but... Why will I probably regret this later? If this is a true statement, I don't know if I want to do this...
You could regret this, because later there could be scripts you don't want to enable.
Oh... Reading now the answer given by getyour411 I understood it. Yeah, and don't think this will be worth it... Hmm... But thanks anyway!
Like... It is going to be an adventure game, but not a point and click one, it's gonna be more like a Luigi's $$anonymous$$ansion game, where you can interact with some stuff in the scenario. Some of them will just have a feedback animation (also to entertain), some will display messages or images on the screen, others will be items that you can get and carry through the game etc. I started doing things using trigger, but I could open a door looking at the wall, just cuz I was inside the trigger. Then I decided to use raycast ins$$anonymous$$d, was it a good decision? And, so far, there is just one code attached on the player managing almost everything (like, if the raycast return a gameobject with the name "keyOne", it will change the interface texture of the slot one or two and diplay the keyOne texture, the gameobject will change its position to look like it's gone and you won't be able to get it again etc).
Thank you all guys for everything so far!
Answer by SkaredCreations · Mar 22, 2014 at 11:37 PM
Honestly it's a strange request to enable ALL scripts (in which case the only way has been suggested by getyour411 in the comment to your topic), I'd suggest to create a basic script for example YourBaseClass (even empty, no variables/methods) that you will inherit in the scripts that you want to target and then use GetComponents of YourBaseClass (it will return both YourBaseClass and inherited scripts)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
what is the problem of my raycast object finder script? 1 Answer
Trouble Rendering Mesh 0 Answers
Custom Collision Detection 4 Answers
physics2d.raycastall help please 1 Answer