#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)

tulip abyss
#

my code looks like this

#

this is actualy the wrong link

#

sorry

dry oyster
#

What line does the error point to?

tulip abyss
#

the instantiate line

#

it is a warning not a error

dry oyster
#

hmmm... that's super strange...

tulip abyss
#

even weirder is that I have this script that works perfectly fine

#

myPrefab is set to the same prefab as card1

dry oyster
#

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 :/

tulip abyss
#

IDE?

#

oh nvm i found out what IDE is

dry oyster
#

code editor ("Integrated Development Environment") 👍

tulip abyss
#

nothing changed

#

actualy now it is giving me an error along with the warning

dry oyster
#

Well that's progress of a sort 😁

tulip abyss
#

The Object you want to instantiate is null.

dry oyster
#

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

tulip abyss
#

i cannot find another copy of the script

dry oyster
#

:/

tulip abyss
#

:/ is right

dry oyster
#

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?

tulip abyss
#

they are litteraly just a trasform

dry oyster
#

hmm

tulip abyss
#

i will try the debugs

#

the prefab is null

#

"Instantiating cardType " + cardType outputed what was expected

dry oyster
# tulip abyss

When you're in playmode, do you see either of the "GameObject" references from this inspector disappear?

tulip abyss
#

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);

dry oyster
#

Interesting... I googled that earlier because I didn't realize that was a valid initialization syntax - but it seems to be 🤔

tulip abyss
#

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

dry oyster
#

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

dry oyster
tulip abyss
dry oyster
#

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.

tulip abyss
#

there cannot be

#

i already serched for it an even manualy serched

#

i dont have many scripts or gamobjects

dry oyster
#

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?

tulip abyss
#

GetInstanceId() doesnt exist in the current context

dry oyster
# tulip abyss ther errors are the same with this

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...

dry oyster
tulip abyss
#

also aparently the array doesnt exist at all

dry oyster
#

What's your code presently look like?

tulip abyss
#

there is another error where i put Debug.Log(cardPrefabs.Length)

#

gimmie a sec

dry oyster
#

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 😅

tulip abyss
#

OMG

#

I am so dumb

dry oyster
#

ah we all are from time to time 😁

tulip abyss
#

this is one of my other scripts

#

it has new Deck in it

dry oyster
#

so it does!

tulip abyss
#

I thought that the error would have directed me that it if there was a problem

dry oyster
#

I totally did as well!

tulip abyss
#

Deck was originaly not a monobehavior

#

i mustve forgot to delete that when i switched it

#

IT WORKS😀

dry oyster
#

I'm glad you got it sorted - that was vexxing 😁

tulip abyss
#

Thank you

#

I was trying to fix it for like 2 hours yesterday

#

Ill put in a good review🤭