You are trying to create a monobehavior using the new keyword but script function fine

Hello everyone,

I’m trying to build a custom map made out of chunks and so far everything was working fine and I could generate my map with each chunk. Now I’m trying to implement a Raycast but since my Chunk class is custom one it was not working so I changed it to Monobehavior.

My scripts are still working fine but I have this

“You are trying to create a monobehavior using the new keyword”

appearing as a warning however everything looks ok and I can even use my raycast now. I know I should use gameobject.addcomponent but I don’ see how I can bring the necessary elements : (viewedChunkCoord, chunkSize,material, mapCreation) when the class is launched or how to add it to my Dictionary.

How would you correct it?

public class UpdateChunk : MonoBehaviour
     Dictionary<Vector3, TerrainChunk> terrainChunkDictionary = newDictionary<Vector3, TerrainChunk>();
void CreateChunk()
      terrainChunkDictionary.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, chunkSize, material, mapCreation));
public class TerrainChunk : Monobehaviour
      public TerrainChunk(Vector3 coord, int chunkSize, Material material, MapCreation mapcreation)
Script to create the game object, place it on the vector 3, create a mesh etc...

Thank you for your help :slight_smile:

The warning comes from calling the TerrainChunk constructor directly.
Instead of new TerrainChunk(..), create a new gameObject and add the component to it.

var newChunk = new GameObject().AddComponent<TerrainChunk>();
newChunk.Initialize(viewedChunkCoord, chunkSize, material, mapCreation);

In this case, you’d have written an Initialize() method that replaces your constructor, to pass the arguments into your instance.
That is, if you decided to turn your TerrainChunk into MonoBehaviour. However, that’s really not a prerequisite for the raycasts to work. Raycasts hit colliders, which are components on GameObjects. In order for raycasts to hit anything, you have to have a gameObject with collider attached. But it’s still perfectly OK to use non-MonoBehaviour classes in your project.

Thank you very very much for your answer. I could manage a solution based on your reply. The issue after applying your solution was that I already created a game object in my custom class so started, the update script had created one gameobject with the creation script and another one with all the mesh etc…
I took off the the game object creation from my (previously custom) class and just add the gameobject compenent to my call

var newChunk = new GameObject("Chunk").AddComponent<TerrainChunk>();
newChunk.TerrainChunkCreator(viewedChunkCoord, chunkSize, material, mapCreation, newChunk.gameObject);
terrainChunkDictionary.Add(viewedChunkCoord, newChunk);

Out of curiosity, now it’s working fine but before my raycast didn’t work even with a collider. If I remember well it mentionned something like only a monobehavior or component script could be retrieved by the raycast. In case of a custom class how would you retrieve for example the transform vector 3 ?