Unity 3b3: PPtr cast failed when dereferencing!

PPtr cast failed when dereferencing! Casting from Shader to Material!

What is this?

Also getting these:

PPtr cast failed when dereferencing! Casting from MonoScript to Material!
UnityEditor.DockArea:OnGUI()

PPtr cast failed when dereferencing! Casting from EditorExtensionImpl to Material!

Is this just happening spontaneously from the editor or does it seem to be caused by one of your scripts? If it’s your script, please can you post it?

Well, it’s happening when I go to game mode, but my scripts were written for me
and I have no idea where the problem might be occurring.
There are multiple background and scene scripts in play.
As far as the scripts I may have to wait until my script writer upgrades to version 3,
he doesn’t want to get involved in the beta so is waiting for the full release.

PPtr cast failed when dereferencing! Casting from Texture2D to Material!

I am getting this after I switch (in code) the material on a MeshRenderer. I switch materials (numbers) on a plane and then renderTexture that out to a GUITexture.

I tried to create a small test scene … but it does not repeat. I have to fix it anyways, so I will upload it once I have it isolated, if you would like.

Hey, great you’ve at least experienced the problem.
Yeah, if you do isolate the thing it would be great to see what you do to fix it.
I do not code but am using scripts to write to hint books and other objects with textures
so I will run it by the guy who writes my scripts for a look.
He hasn’t done any debugging on this and I haven’t mentioned it to him, not knowing what it is.
I’m wondering about all these OnGUI() errors, if it’s not something that’s changed in how the code should be written.

Hey PrvtHudson,
Have you managed to isolate the problem?

I just got

PPtr cast failed when dereferencing! Casting from Shader to MonoBehaviour!

During lightmapping, and in beta 4.

I also have the same thing!

I think it comes from the Types not matching from function to function. I’ve had a small issue today where I changed the Type of some variables from Transforms to GameObjects. The Inspector still saw my variables as Transforms even after I’ve changed them.

Does anyone else notice this error? Does it have to do with the win64 hotfix by any chance?

Thanks in advance for any clues!

You nailed it! I came here looking for the answer. I had a script with a generic list of GameObjects (C#: List). In the editor, I drag&dropped a few items in to the list. Later I change the type of the list to Transform and I got the error. After lots of wasted time, I found your post. THANK YOU! I should have noticed the icons in the Editor were not transform icons. I just drag and dropped again and now the icons are right and it shows “(Transform)” after the name.

i get this in the editor when entering a variable in unity

PPtr cast failed when dereferencing! Casting from Texture2D to MonoBehaviour!

Happens at least a few times a day with 4.1 + as well. I don’t see how we can reproduce it when it happens on completely entirely different codebases as well.

On Mac for what it’s worth.

For the record I get this occasionally too. Can’t work out what causes it but just running again usually fixes it - it’s pretty intermittent. It changes what it thinks it’s casting from and to, although usually it’s casting “to Shader”.

Recently I got “casting from AssetBundle to Shader” and I don’t even have ant AssetBundles in this project, and never have.

On PC, Windows 7, running Unity 4.2, but have seen this since at least 4.0.

I get this too, but only sometimes.
For me it happens somewhere around Resources.LoadAll as the error message states.
I’m just wondering, as nearly all of the time it runs flawlessly.
Though i have the feeling that it happens more often when i play/end game mode repeatedly.

Oh yeah, it only happens to me right as I run the game in the editor, never during. If it doesn’t do it right away it’ll never do it.

I get:

PPtr cast failed when dereferencing! Casting from DefaultAsset to Shader!
UnityEditor.UnityBuildPostprocessor:OnPostprocessScene()

Help me getting rid of these!

I’m getting them too:

This started happening somewhere around the 4.x’s. Currently on 4.2.1. I have a ton of code in my project and wouldn’t know where to begin looking.

It would be super damn helpful if Unity could add a line number or at least mention which object these are happening on. :slight_smile:

Wait, I’m full of poo. I searched for casts to ProceduralMaterial, and while I found none in my game scripts, I found one file in my editor directory. I commented it out, and the errors are gone.

Here’s the offending class. The problem is somewhere in OnGUI():

I get the PPtr error when I try to reference the mesh of a gameobject:

_fog_mesh = _fog_of_war_object.GetComponent( ).mesh;

but the weird thing is that it happens only very sporadically and that it’s very hard to reproduce.

On Unity 4.2.2f1 I’m getting

It happens sporadically while in the Editor when calling

var instance = PrefabUtility.InstantiatePrefab(somePrefabReference);

At first I was casting the result myself to my own MonoBehaviour derived type. I modified the logic thinking I was the cause of the error, but it persists and keeps pointing me to a line of code where I’m not doing any casting at all, so I’m fairly certain this is an internal Unity error.

Other than it freaking me out it doesn’t seem to actually break anything.