Search Unity

Unity 3b3: PPtr cast failed when dereferencing!

Discussion in 'Editor & General Support' started by Don-Gray, Jul 21, 2010.

  1. Don-Gray

    Don-Gray

    Joined:
    Mar 18, 2009
    Posts:
    2,278
    PPtr cast failed when dereferencing! Casting from Shader to Material!

    What is this?
     
  2. Don-Gray

    Don-Gray

    Joined:
    Mar 18, 2009
    Posts:
    2,278
    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!
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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?
     
  4. Don-Gray

    Don-Gray

    Joined:
    Mar 18, 2009
    Posts:
    2,278
    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.
     
  5. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    Code (csharp):
    1. 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.
     
  6. Don-Gray

    Don-Gray

    Joined:
    Mar 18, 2009
    Posts:
    2,278
    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.
     
  7. Don-Gray

    Don-Gray

    Joined:
    Mar 18, 2009
    Posts:
    2,278
    Hey PrvtHudson,
    Have you managed to isolate the problem?
     
  8. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I just got

    Code (csharp):
    1. PPtr cast failed when dereferencing! Casting from Shader to MonoBehaviour!
    During lightmapping, and in beta 4.
     
  9. Angrycrow

    Angrycrow

    Joined:
    Jan 29, 2011
    Posts:
    34
    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!
     
  10. Rafe

    Rafe

    Joined:
    May 4, 2010
    Posts:
    4
    You nailed it! I came here looking for the answer. I had a script with a generic list of GameObjects (C#: List<GameObject>). 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.
     
  11. moinchdog

    moinchdog

    Joined:
    Apr 4, 2012
    Posts:
    15
    i get this in the editor when entering a variable in unity



    PPtr cast failed when dereferencing! Casting from Texture2D to MonoBehaviour!
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  13. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    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.
     
  14. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    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.
     
  15. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    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.
     
  16. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    I get:

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

    Help me getting rid of these!
     
  17. Dave-Taylor

    Dave-Taylor

    Joined:
    Jul 7, 2012
    Posts:
    21
    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. :)
     
  18. Dave-Taylor

    Dave-Taylor

    Joined:
    Jul 7, 2012
    Posts:
    21
    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():

     
  19. deleo

    deleo

    Joined:
    Oct 4, 2013
    Posts:
    4
    I get the PPtr error when I try to reference the mesh of a gameobject:

    _fog_mesh = _fog_of_war_object.GetComponent<MeshFilter>( ).mesh;

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

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    On Unity 4.2.2f1 I'm getting
    It happens sporadically while in the Editor when calling
    Code (csharp):
    1.  
    2. var instance = PrefabUtility.InstantiatePrefab(somePrefabReference);
    3.  
    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.
     
    Last edited: Nov 3, 2013
  21. thegdeveloper

    thegdeveloper

    Joined:
    Feb 25, 2013
    Posts:
    134
    I have faces the same Issue. Although my game is not crushing, I just get the error message on the editor console
     
  22. Mr_Artistic

    Mr_Artistic

    Joined:
    Dec 30, 2013
    Posts:
    1
    Im really new to Unity, like downloaded it yesterday, and I am getting the error "Dereferencing NULL PPtr! UnityEditor.AnimationUtility:GetAnimatibleBindings(GameObject, GameObject)" in my console. Its the newest form of Unity (4.3). Can anyone help?
     
  23. Jakester1013

    Jakester1013

    Joined:
    Dec 28, 2013
    Posts:
    1
    Hello everyone!

    In case that you are still wanting to get rid of the error message(s), or if you came here looking for the solution to this problem, I have answers!

    Cause of the issue: This occurs when you change the variable type from Shader to Material but don't change the variable name. Unity likes to throw a hissy fit like that when one changes a variable type to another in script. Why Unity does this is beyond me.

    Solution: Change the variable name! I did that and it solved the problem for me! :)

    Hope this helps!
     
  24. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    In my case, Unity tells me that PPtr case failed when casting from something-or-other to shader, but in fact I'm not casting to a shader anywhere in the project at all. A strange one.
     
  25. pedrord88

    pedrord88

    Joined:
    Oct 28, 2013
    Posts:
    1
    It worked for me!! I commented the line of the declaration of my var and then I declared the same var with other type, and then this error appeared:
    PPtr cast failed when dereferencing! Casting from Texture2D to Sprite!
    But when I changed the name to the variable the error was fixed!
    Thanks!
     
  26. TophWells

    TophWells

    Joined:
    Feb 24, 2016
    Posts:
    1
    I managed to solve this one in my project. I'd changed the type of a particular variable, and for some reason the Inspector didn't properly account for this. You can actually see the weird behaviour on this screenshot:



    Renaming the offending variable fixed the problem. Dragging the correct resource onto the inspector didn't fix the problem, but did hide this particular symptom, so it might not always be so visible.

    In my case, the error always happened when I called a particular function that used the glitched Sprite, and always mentioned casting GameObject to Sprite. That sounds different from what some of the users above me have reported, so maybe the cause isn't always the same.
     
  27. Monica_L22

    Monica_L22

    Joined:
    Dec 28, 2018
    Posts:
    2
    I got this error PPtr cast failed when dereferencing! Casting from PrefabInstance to Prefab.
    when I imported models (tried both from other Unity project and Asset Store) to Unity 2018.3.8f1, and all my material was lovely pink. I tried the package in Unity 2018.2.2f1 and it's working fine.

    Sent a screenshot to my friend and he gave me a solution.

    Fixed by updating the material: https://github.com/Unity-Technologies/ScriptableRenderPipeline/wiki/Upgrading-to-HDRP

    Cheers.
     

    Attached Files:

  28. estantaya

    estantaya

    Joined:
    Feb 24, 2014
    Posts:
    1
    this is a user error thats why happens in all versions and have no answers by the unity team xd, i have a variable Transform with a reference but if change that variable type for other in my case Light and use that variable that's trigger the error :p
     
  29. ilyavolkov

    ilyavolkov

    Joined:
    Feb 23, 2017
    Posts:
    1
    Thank you so much! This worked great for me.
     
  30. mattCap42

    mattCap42

    Joined:
    Jul 25, 2022
    Posts:
    7
    What if the errors are being generated by a Unity script like TMP_PerBuildProcessor ?

    PPtr cast failed when dereferencing! Casting from GameObject to AnimationClip!
    UnityEditor.AssetDatabase:LoadAssetAtPath<TMPro.TMP_FontAsset> (string)
    TMPro.TMP_PreBuildProcessor:OnPreprocessBuild (UnityEditor.Build.Reporting.BuildReport) (at Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Editor/TMP_PreBuildProcessor.cs:21)
    UnityEditor.BuildPlayerWindow:BuildPlayerAndRun ()
     
  31. dibsoftwares

    dibsoftwares

    Joined:
    Mar 24, 2019
    Posts:
    3
    verifique se ha algum Reflection Probe com tamanho acima do limite das texturas dos objetos
     
  32. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    Fourteen years of this error, and they still can't be bothered to add an object reference to the console so you can know what object is causing it. This should be an inviolable design law across Unity: an error message MUST have a name or a ping-able reference to the specific object or asset or source file with a problem.

    In my case, a prefab variant which had an unpopulated LODGroup component gave me two of these errors for each instance in a scene. "Cannot cast from Shader to Renderer!"