Leaving texture[mark] on collision.

Hi all, i want to make a texture/mark being left in the place where the ball[player] colided with. For example i want when player the ball hits the box it will instantiate a plane which i put a texture onto it . i have made something like this but apparently it says an error ‘’ NullReferenceException: Object reference not set to an instance of an object’’

[this script is attached to the box var Woodhole : GameObject[ ]; var floatInFrontOfWall : float = 0.00001;

  • functionOnCollisionEnter(hit:Collision){
  • if(hit.gameObject.tag ==“Ball”&&lock==0){
  • Instantiate(Woodhole[Random.Range(0,3)], hit.point +(hit.normal * floatInFrontOfWall),Quaternion.LookRotation(hit.normal));
  • }
  • }

Thanks for helping !

These are usually referred to as decals. It’s a bit of a complex subject, simple planes will work in some situations, but what if the ball hits on a corner? Or a part of the mesh that isn’t flat? There are a number of decal solutions on the asset store you can look at.

It’s difficult to tell exactly what’s wrong with your script, though. I suspect that Woodhole is null, how are you initializing this array?

1 Like

Hi there, i have another problem idk if its just me or this engine is annoying [ probably me because i dont know much]
But why the hell if i say play one shoot it doesnt play one shoot it just plays an explosion sound like looping ?? like really look here is what i wrote
function Update ()
{
if (boxd==true)
{
GetComponent.().PlayOneShot(boxexplosion);
}

}

no point creating new threads cuz i just wanted to implement this while having another problem and i got new problem

There is a Loop setting on your AudioSource component, it’s probably checked. When you’re first starting out you’re going to have a lot of problems like this. It’s the same in any game engine, there’s just a lot of stuff to learn, a lot of interactions you didn’t anticipate, etc. Keep at it, things get much less annoying as you get experience.

The problem is that im working with unity for about year now and i could say im at 3.5/10 stage of knowing all about unity so im not beginner like really beginner i know stuff but sometimes its frustruating .
I checked and the loop is unchecked on audio source and it still plays sounds looping until it gets destroyed .

Is there more than one audio source attached to your game object? If there’s more than one audio source, a GetComponent() will grab the first one in the list (I think) and if you’re changing the settings on another one it won’t work.

I don’t know what to tell you other than “keep at it.” Look around and you’ll find what’s wrong. If you really can’t figure out what’s wrong, make a new scene and try to do what you want to do as simply as possible. Sometimes it’s working as you expected, but some interaction or some script isn’t working as you thought it would. Looking at it in isolation in a new scene can really help.

The problem with the sound issue is that you are running it from update, which is called every frame. The sound is not looping, the sound is being played every frame because the if statement is true every frame because you are not setting it to false anywhere. You should be playing the sound from the OnCollisionEnter function.

For the first problem, are you dragging the game object into the field on the inspector?

Hi there, so i made it differently than yesterday i have made it in the box script and not in player script, so the script is attached to box, it ‘kinda works’ cuz still not what i want, its not looping all time but it loops or plays this sounds 5 times at once like its echo or something [more like it played couple of the same sounds at once]
Here is what i wrote

     function Update(){
        if(health <= 0){
        DestroyOb();
          
        }

     }
    
    
     function DestroyOb ()
    
     {
     Instantiate(effect, transform.position, Quaternion.identity);
     AudioSource.PlayClipAtPoint(destroy, transform.position);
     yield WaitForSeconds (0.2);
     Destroy(gameObject);
    
     }

I have i think fixed the issue with sound i wrote

var boxdead : boolean;
function Start () {
     boxdead = false;
}
function Update(){
        if(health <= 0){
            DestroyOb();
            if(!boxdead)
            {
                boxdead = true;
                GetComponent.<AudioSource>().PlayOneShot(destroy);
            }
       
        }

But the sound now plays really quiet and the audio source is on max any idea :?
Edit 1 : The sound is bad looks like it just ends in the middle and there is no fadeout as the clip has idk im so confused with this audio all in this unity
Edit 2: I fixed it :smile: by changing to playat point and now it sounds as it should finally :smile:
Now need to try to make the decal system work like when the ball hits the box it will leave a mark on it also, when the ball rolls on the floor it will leave mark any idea guys :?

Decal system too complicated so i made a texture that looks ok like have a crack so when ball hits the box it changed to that texture with crack and then one more hit and it destroys