I’m pretty new to scripting in Unity and have been getting an ArgumentException error saying that I’m not allowed to call Internal_CreateGameObject when declaring a variable. This error happens on initialization only and doesn’t actually affect the functionality of my scripts. It’s really just an annoying error right now, but I know it’ll probably be more than that if I don’t solve it now. I’ve been searching around on the forums about why this is happening and haven’t been able to find anything. Could anyone help with telling me why this Arg exception comes up?
I think, but could be wrong, it has something do with creating serialized fields which I am doing like this;
[SerializeField]
GameObject goConnector = new GameObject();
Thanks in advanced!
I’m guessing you need to put this in one of the initialization methods (Awake or Start) instead:
private GameObject goConnector;
private void Awake()
{
this.goConnector = new GameObject();
}
Thanks for the suggestion. I gave that a try, but it did not clear up the error. It actually was a little bit worse. Not only did I still have the error, but it created new game objects the flooded my hierarchy at the beginning. I’ll try to attach my .cs file and maybe that will help clear up the issues.
450502–15705–$Connectors.cs (1.78 KB)
Well it looks like you’d be creating new game objects for each instance of Connectors.
Also by looking at your code, I think, perhaps that you misunderstand declaration, initialization, and nulls.
//Creates a list to put the connecting way points in.
List lConnectors = new List();
//Enables me to use the list
lConnectors = lConnectors ?? new List();
There’s no need to check for null (lConnectors ??) and then reassign it a new instance. It’s already been declared and assigned a new instance from your first line.
Similarily, I’m guessing from your comments that FirstConnector through FourthConnector are assigned in the inspector. Therefore your declarations should be:
//Gets the connected waypoint from Inspector
[SerializeField]
GameObject FirstConnector;
[SerializeField]
GameObject SecondConnector;
[SerializeField]
GameObject ThirdConnector;
[SerializeField]
GameObject FourthConnector;
Then have those assigned through the inspector – you shouldn’t need to call “new GameObject()” on them.