- Home /
RayCast Hit
I have a script that I am using for a game. I want it to change a cubes (a cube will be referenced as a block from here) texture when it is clicked with the left mouse button. There are multiple blocks on the screen and I want only one. Here is the code:-
var hit : RaycastHit;
var BlockTexture : Texture;
function Update () {
if(Input.GetMouseButtonDown(1))
{
TextureBlock();
}
}
function TextureBlock()
{
if(HitBlock())
{
cube.renderer.material.mainTexture = BlockTexture;
}
}
function HitBlock() : boolean
{
return Physics.Raycast(transform.position, transform.forward, hit, range);
}
Where am I going wrong. I cannot figure out where I am going wrong. The errors I am getting are:-
Assets/CraftingTest/RayCastHit.js(15,17): BCE0005: Unknown identifier: 'cube'.
And
Assets/CraftingTest/RayCastHit.js(21,31): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.RaycastHit, function(int): System.Collections.Generic.IEnumerable.)' was found.
Answers are appreciated.
Answer by LucasMars · Jul 14, 2013 at 02:13 PM
Turns out that I was doing it all wrong... Here is the code I used and it worked:-
function OnMouseDown () {
this.gameObject.renderer.material.color = Color.red;
}
Answer by Slobdell · Jul 13, 2013 at 01:13 PM
Well one is that cube is an unknown variable. Assuming that your gameobject is named cube, you can remove it, or change it to this
renderer.material.mainTexture = BlockTexture;
or
this.renderer.material.mainTexture = BlockTexture;
Second, have you defined "range"? I looks like you haven't and the compiler thinks you are trying to call a method. Try changing range to a number like 1000.
Thanks for your answer. That kind of worked. There was a problem. When I clicked, it would set half the objects to the quick texture I made. And then it would not set any more blocks. The picture will show you what I mean...
[Edit] I had to edit this because Google Chrome wouldn't upload the picture. [/Edit]
ha, ok new solution for you. Change your hit method to something like this
function HitBlock() : boolean
{
Bool hit$$anonymous$$e = false;
if(Physics.Raycast(transform.position, transform.forward, hit, range)){
if(hit.collider == this.collider){
hit$$anonymous$$e = true;
}
}
return hit$$anonymous$$e;
}
Right now you're checking if the raycast hit anything, anything at all. You need to check which object it hit
Also, if this wasn't changing every object I'd guess you forgot to attach the script to the objects that didn't change
The script just gives me this message:-
Assets/CraftingTest/RayCastHit.js(22,10): UCE0001: ';' expected. Insert a semicolon at the end.
But there is a semicolon at the end. I don't know what to do.
Your answer
Follow this Question
Related Questions
Change crosshair text when hitting a trigger 0 Answers
Hit distance Change light range 1 Answer
How to hit two objects with only one shot (raycast) 0 Answers
Raycast & AI problem. 1 Answer