Where is the mistake?

Getting an error… Looks exactly the same as on the lecture screen, but still an error.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ObjectHit : MonoBehaviour

{

private void OnCollisionEnter(Collision other)

{

    if(other.gameObject.tag == "Player")

    {

    GetComponent<MeshRenderer>().material.color = Color.red;

    }

}

}

1 Like

Hi,

Check if you have got multiple ObjectHit scripts in your Assets folder.

1 Like

It keeps on creating the second one by it self each time I hit a play button. I remove it manually, hit play. Object falls and turns red on falling, not when player hits in to it. So, I am stuck completely.

That sounds strange. I’m not sure if I understood you correctly. Actually, unless you used something like AddComponent or Instantiate in your code, nothing should be created when you hit the play button.

Since you are using an older version of Unity: Perhaps you are experiencing a bug. I would suggest to update to the latest stable version.

This happened to me also. Out of nowhere there is a second Scorer.cs in the top level Assets folder. I am using the latest Unity. I deleted it and it hasn’t reappeared.

I suspect that the culprit is VS Code. If you create a script then move that script to a new folder (like what Rick does sometimes later in the lectures) but you don’t close and re-open VS Code it will keep the moved script in it’s tabs with the label “deleted”. I think it can re-save that file and it suddenly reappears in it’s orginal location. VS Code doesn’t know you moved it. VS Studio might be more intelligent or at least better integrated with Unity to understand a file has been moved.

This happened to me too, and it is because after he had us move all of the CS files to the script folder in a previous lesson, we still had them open in Visual Studio Code - so it saved a new version in the old place.

We updated ObjectHit.cs (deleted) and used CTRL + S, which re-saved it to the old location. In my screenshot, you can see that my Mover.cs, and Scorer.cs still remain as (deleted), after I CTRL + S’d my deleted ObjectHit.cs it resaved it in the old location.

So, we just need to remember that if we move .CS files, we need to close all of the windows we have open in Visual Studio Code.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms