#You are trying to create a MonoBehaviour using the 'new' keyword but Im not using new at all
1 messages · Page 1 of 1 (latest)
What line does the error point to?
hmmm... that's super strange...
even weirder is that I have this script that works perfectly fine
myPrefab is set to the same prefab as card1
You might restart the IDE just to make sure it's language features haven't gotten caught in some weird state... And make sure there are no other compilation errors which might have prevented Unity from using the updated script :/
code editor ("Integrated Development Environment") 👍
Well that's progress of a sort 😁
The Object you want to instantiate is null.
ah... so I guess double check that card0 and card1 are both set in the inspector... And maybe check your scene to ensure that there's no extraneous copies of this script which shouldn't be there, and may not have been configured - you can search your hierarchy for "t:Deck" to find all of the game objects which have this component
:/
:/ is right
I guess maybe time to sprinkle in some Debug.Log()s... just to make sure the values make sense... I'm not sure why they wouldn't - but it can't hurt to check.
Debug.Log("Instantiating cardType " + cardType);
if (prefabArray[cardType] == null)
Debug.Log("prefab is null!");
Instantiate(prefabArray[cardType], new Vector3(0, 0, 0), Quaternion.identity);
I totally feel like this code alone shouldn't have created that warning... do these "GameObject" prefabs have any scripts attached?
they are litteraly just a trasform
hmm
i will try the debugs
the prefab is null
"Instantiating cardType " + cardType outputed what was expected
When you're in playmode, do you see either of the "GameObject" references from this inspector disappear?
nope
i have another idea give me a sec
the problem is here GameObject[] prefabArray = { card0, card1};
i added this below it Debug.Log(prefabArray[0].name + " " + prefabArray[1].name);
Interesting... I googled that earlier because I didn't realize that was a valid initialization syntax - but it seems to be 🤔
it came back with an error Object reference not set to an instance of an object at the debug line
is there a better way to create an array of prefabs
maybe it has somthing to do with the array bieng local
even if the gameobjects themselves are public
I think it should all be fine. You could try a more verbose initialization syntax, like
GameObject[] prefabArray = new GameObject[] { card0, card1 };
But you still might throw a
Debug.Log(card0.name + " " + card1.name, gameObject);
above your other log... The second gameObject argument will link the log such that clicking it will highlight the corresponding object in the hierarchy
Ultimately you might want to just expose something like a
public GameObject[] cardPrefabs;
field on the script, rather than constructing the array from individual fields - that'll give you a nice list in the inspector
it threw another error at the new debug line so ill use this
Ah alright - that's sort of what I was curious about. It was a null reference exception, yeah? That would mean that card0 and/or card1 were null. I think there's a second copy of the script, somewhere, and it's not configured.
there cannot be
i already serched for it an even manualy serched
i dont have many scripts or gamobjects
Hmmmm
So a couple thoughts, one way to check for sure would be to add
void Start() {
Debug.Log( "Draw " + GetInstanceId(), gameObject );
}
to the script - that log should only print once, with one ID...
My only other thought presently is that maybe these "GameObject"s are referencing an actual instance of the prefab from the scene hierarchy instead of the "GameObject" prefab asset file from the Projects window, and perhaps something is Destroy()ing the ones in the scene at some point?
ther errors are the same with this
GetInstanceId() doesnt exist in the current context
Yeah... I reckon if you Debug.Log(cardPrefabs.Length) somewhere prior to the error it would log 0, despite setting it up the inspector... I'm sort of convinced it's not really the code that's the source of the problems, but rather that the references are null for some reason - and the only causes I can think of for that are that the referenced game objects get destroyed at some point, some other code is changing the references, or there are two copies of the script...
Oh that's super strange - it should totally be available in a MonoBehaviour class 🤔
also aparently the array doesnt exist at all
What's your code presently look like?
So that all seems fine. Normally that error would be correct, because with just
public GameObject[] prefabArray;
we're just declaring the variable and not initializing it. But Unity initializes default values for public and serialized fields on MonoBehaviour components for us.
It almost sounds like you have new Deck() in your code somewhere
...which I think might make all of the errors make a lot of sense 😅
ah we all are from time to time 😁
so it does!
I thought that the error would have directed me that it if there was a problem
I totally did as well!
Deck was originaly not a monobehavior
i mustve forgot to delete that when i switched it
IT WORKS😀
I'm glad you got it sorted - that was vexxing 😁