x


Global Function in C# in Unity

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:

using UnityEngine;
using System.Collections;
using System;

//Global Function that Creates game object 
public class CreateGameObject
{
    public static void CreateNewGameObject(object newGameObject, Type gameObjectType, string gameObjectName)
    {
       GameObject go = new GameObject();
       gameObjectType = newGameObject.GetType();
       newGameObject = go.AddComponent<gameObjectType>();
       go.name = gameObjectName;
    }
}

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.

more ▼

asked Sep 05 '11 at 10:28 PM

AbsurdHart gravatar image

AbsurdHart
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
"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."

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:

public static void CreateNewGameObject< T >( GameObject go, string name) where T : Component
{
   GameObject newGo = Instantiate( go );
   newGo.AddComponent< T >();
   newGo.name = name;
}

You'd call this function like: CreateGameObject.CreateNewGameObject< Renderer >( this.gameObject, myClone ) which would clone this gameObject and add a renderer.

more ▼

answered Sep 06 '11 at 06:06 AM

Joshua gravatar image

Joshua
6.4k 19 25 70

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)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4154

asked: Sep 05 '11 at 10:28 PM

Seen: 659 times

Last Updated: Sep 06 '11 at 06:51 AM