Ok, I am trying to make it so that I can flip a switch an open a door. Simple, done. Now I want to have several switches and several doors, but not several scripts. I am also trying to do this with raycasting as it is for a first person game.
Here is the script I have:
var shootDistance = 100.0;
function Update () {
Screen.lockCursor = true;
if (Input.GetKeyDown("e")){
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if(hit.transform == gameObject.FindWithTag("switch")){
Fire();
}
}
}
}
function Fire(){
print("good");
}
So If the raycast hits an object with the tag “switch” it should activate the proper function and print “good”. However nothing happens.
If I can get this to work I was trying to find a way that I could have the raycast send back which object it hit so I could have it change a variable on the hit object. something like:
If all of that could work then I would only need one this one script to interact with every object in my game… I just hope that’s not wishful thinking.
function Fire (){
silly = objectHitByRaycast.GetComponent("Script");
silly.Variable = true;
}
But that also makes me wonder if i can have a variable in place of the “Script” so that that can be interchanged as per object the script is on.
I really like using variables for everything.
the code should works correctly and print “good”
but maybe you don’t place the mouse over the object that has the switch tag or maybe that object don’t have collider in it’s size or …
check them.
also you can print the name of the object referenced by “hit” variable in you raycast and see what object will get the ray. or you can draw the ray to see where it goes with debug.drawray function
hope this helps
Thanks! ill give that a try. I am getting frustrated with the tag. I am going to try some one or two of the premade tags or a new one.
No luck with the tag. Tried quite a few different colliders. a few different tags, with trigger on and with trigger off.
Could you post an example of printing which object the raycasting is hitting? simply print(hit); doesnt work. I am still pretty new to raycasting. I need to read up more on it.
you should get the transform or collider or rigidbody of hit and then get it’s name
Print (hit.transform.name)
Ok, so thank you so much for that one line of script, it opened up a complete workaround! I still hit a brick wall with scripts not working for no reason!
Here is my work around, I am pretty happy with it.
if (Physics.Raycast(ray, hit)) {
ObjName = hit.transform.name;
ObjTag = hit.transform.tag;
if(ObjTag == "Switch"){
ObjFound = GameObject.Find(ObjName);
ObjVar = ObjFound.GetComponent("Script_DoorSwitch");
ObjVar.doorOpen = true;
}
Then on the object that was hit with the raycast i have a very simple script.
var doorOpen = false;
var door : GameObject;
function update(){
if(doorOpen == true){
print("poopnuggets");
silly = door.GetComponent("Script_ThickSmallSlide");
silly.flip = true;
doorOpen = false;
}
}
I can see the variable change from false to true (unchecked to checked) in the inspector, however the if statement is never triggered, and poopnuggets is never printed.
I am at a complete and utter loss right now.
dear friend!!!
the problem is easy to solve.
first of all when you can get the transform of an object or it’s rigidbody or collider, you have the reference to it. you don’t need to find it using gameobject.find
you can access it’s gameobject by
hit.transform.gameobject
i told that you can use gameobject.find if you want to find all objects with a specified name
gameobject.findall()
Do you know why your code don’t work?
…
gameobject.find returns one of the objects named objname but it can be any of them. if you want to see if it’s the spesific one that you want you should check it’s position or any other unique property.
but as you understand you don’t need this just use
GameObject onj = hit.transform.gameobject;
to get a reference to your gameobject and then
obj.GetComponent(“Script_Door_switch”);
to access your scripts
P.S odn’t use names with underscores and use DoorSwitch. it’s the best standard and .NET standard and the inspector shows them well
hope this help
wow, I am blown away by how much simpler the script can get. I feel that sometimes my love of using variables gets the better of me. I am taking your advice and renaming all of my scripts to take out the underscore.
I changed up the script as to your advice.
obj = hit.transform.gameObject;
obj.GetComponent("DoorSwitch").doorOpen = true;
I am still having trouble with the DoorSwitch though. Which doorOpen does become true, the update doesnt recognize it.
I realized I was using a lower case u, and changed that to U for the Update. Still no luck. What makes it more strange is function start and awake wont work either.
Thank you very much for the little lesson in raycasting!
take a look at your Update function, it should have uppercase U and Start should have uppercase S and Awake should not be written as awake
something like this
function Update()
{
}
and then check if your script inherits from monobehaviour if it’s a C# script.
then check if you attached the script to your gameobject or not. then check if it’s enabled or not in the inspector. (there is a checkbox for it)