|
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.
(comments are locked)
|
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. I didn't know how to declare a function with Generic function parameters before this, so that's useful. :) I have a question, though. I don't understand how this method actually returns anything. The parameter go is passed by reference, and then based on that reference, you instantiate a clone and save a reference to the clone in a new method variable. The clone has the same name (go) as the parameter, but that reference doesn't point to the same GameObject as the parameter anymore after the cloning; it's simply a new object. So, you can add T and give it a name, but won't it leave scope after the method returns and get garbage collected? I think you need to declare this method's return type "GameObject" instead of void, and return go. (The clone, not the parameter).
Sep 06 '11 at 06:27 AM
CHPedersen
You have a point that it was sloppy not to declare a new variable for the new go, although it doesnt matter. And it would make more practical sense to return a reference to the new go, but I was trying to keep my example close to his.. ;) Now it doesnt matter for the gc, als go's do not get gc'd, you,have to explicitly call Destroy.
Sep 06 '11 at 06:51 AM
Joshua
(comments are locked)
|
