- Home /
Gameobject as Optional Argument
I was doing a major rewrite of some procedural generation code and I encountered somewhat of a brick wall. I need to have a GameObject as an optional parameter:
public GameObject Generate( float radius, //These parameters generate a circular frag
int vertices,
float degVar = 0.0f,
Color? color = null,
Vector3? pos = null,
Quaternion? rot = null,
GameObject? obj = null)
{
GameObjectfrag = obj.HasValue ? obj : (GameObject)GameObject.Instantiate (dupliFrag); //Instantiate prefab object if no object to be modified is provided.
}
This is just the snippet of code that's affected. This throws the following error:
Assets/FragmentGenerator.cs(29,46): error CS0453: The type `UnityEngine.GameObject' must be a non-nullable value type in order to use it as type parameter `T' in the generic type or method `System.Nullable<T>'.
I can't for the life of me figure out a clean workaround for this. One solution would be to use polymorphism, but I feel like there must be a more concise way to do this.
GameObject is already a nullable type, so using GameObject?
causes the error.
Anything stopping you from just saying GameObject obj = null in the parameters and then doing a plain if(obj == null)in code?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Setting Prefab with Code Returns NULL 2 Answers
Simple Looping drag and drop game 1 Answer
Distribute terrain in zones 3 Answers
I have an error, script should check for null or not destroy game objects 2 Answers