Spray Painting?

want to make a City themed Game. So i need the Character to be able to spray on walls.

I have this Code:

var step:GameObject;
var bloodyStep:GameObject;
var ok:boolean = true;

function Update(){
if(Input.GetKeyDown(KeyCode.Space))ok=!ok;
while(Input.GetMouseButtonDown(0)){
var ray : RaycastHit;
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition),ray)) {
if(ok){
var steps=Instantiate (step, ray.point, Quaternion.identity);

}else{
var blSteps=Instantiate (bloodyStep, ray.point, Quaternion.identity);

}
}
}
}
But if i click, the Game crashes! How can i fix that?

Please, use

 tags :neutral:

Try this
[CODE]var step:GameObject;
var bloodyStep:GameObject;
var ok:boolean = true;

function Update(){
	if(Input.GetKeyDown(KeyCode.Space))ok=!ok;
	if(Input.GetMouseButtonDown(0))
	{
		var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var hit: RaycastHit;
		if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
			if(ok){
				var steps=Instantiate (step, hit.point, Quaternion.identity);
			}else
			{
				var blSteps=Instantiate (bloodyStep, hit.point, Quaternion.identity);
			}
		}
	}
}

okay, but i want to “spray” when i hold the mouse button
so i dont must click any pixel once

just change

if(Input.GetMouseButtonDown(0))

to

if(Input.GetMouseButton(0))

Ok thanks but only last Problem
The Cubes (the spray paint effect) spawns at XYZ = 0
How can i do, when i click on a wall, that the paint goes there?

I don’t know, the object I instantiate in test project spawn at hit point… Here’s my test project so take a look.

1188800–46541–$Test.rar (161 KB)

Still doenst work.
It only spawns on the Terrain around 0.0.0
Not on Walls or anything

Be sure the object you’re hitting isn’t in layer “Ignore raycast”… I can’t do much from information “doesn’t work”…

Doenst work means that the Cubes still spawn at 0 0 0

Man i need this so badly, has no one a idea.
I tried everything

Before this line:

if(ok){

Add:

print(hit.transform.name);

So you should see what object the ray hits…

So i got i nearly.

If i “Spray” now, the cubes are transformed left a little bit.
How csn i fix this?

you have PM.

Did you find a way to fix it?