I'm trying to create a global function that creates game objects and attaches components based off of the type passed into the function, such as this code:
I get this compiler error: Error CS0246: The type or namespace name `gameObjectType' could not be found. Are you missing a using directive or an assembly reference? (CS0246) (Assembly-CSharp)
The goal is to have a global function that takes in any object type and attaches a component based off its type.
I was wondering if someone could suggest something I'm doing fundamentally wrong here, I'm sure I am, I'm just a beginner and am not sure exactly myself. Thank you.
asked Sep 05 '11 at 10:28 PM
What you're doing fundamentally wrong here is that Type is not a variable, it's a Type.
There is a way to do exactly what you want to do though, they're called Generic function parameters. I didn't understand the first parameter of your function, are you trying to clone a gameobject and attach a component? That's what this'll do:
You'd call this function like: CreateGameObject.CreateNewGameObject< Renderer >( this.gameObject, myClone ) which would clone this gameObject and add a renderer.