- Home /
The question is answered, right answer was accepted
OnCollisionEnter causes crash
Hello!
The device should vibrate when I hit a wall, but when I hit one the game crashes. It doesn't crash if I comment out the OnCollisionEnter method, but it does crash if I only comment out
if (Wall.gameObject.tag == "Wall") {
if (PlayerPrefs.GetInt ("VibrateIsOn") == 1) {
Handheld.Vibrate();
}
Commenting out this didn't help either:
collision = true;
So only when I comment out the whole OnCollisionEnter -method it doesn't crash
THIS IS THE FULL SCRIPT:
using UnityEngine;
using System.Collections;
public class CollisionTrigger : MonoBehaviour
{
public GameObject Wall;
public static bool collision = false;
void Start ()
{
collision = false;
}
void OnCollisionEnter ()
{
if (Wall.gameObject.tag == "Wall") {
if (PlayerPrefs.GetInt ("VibrateIsOn") == 1) {
Handheld.Vibrate();
}
collision = true;
}
}
}
Crash log:
thread #1: tid = 0x1519, 0x01049608 mazeracing`il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(TypeInfo*, unsigned int, bool) [inlined] il2cpp::metadata::SZArrayClassHash::operator(arrayClass=0x00000000)(TypeInfo const*) const at ArrayMetadata.cpp:379, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x28)
frame #0: 0x01049608 mazeracing`il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(TypeInfo*, unsigned int, bool) [inlined] il2cpp::metadata::SZArrayClassHash::operator(arrayClass=0x00000000)(TypeInfo const*) const at ArrayMetadata.cpp:379 frame #1: 0x01049608 mazeracing`il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(TypeInfo*, unsigned int, bool) [inlined] std::__1::__unordered_map_hasher, il2cpp::metadata::SZArrayClassHash, true>::operator(__x=0x00000000)(TypeInfo* const&) const at unordered_map:384 frame #2: 0x01049608 mazeracing`il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(TypeInfo*, unsigned int, bool) [inlined] std::__1::__hash_iterator, void*>> std::__1::__hash_table, TypeInfo*>, std::__1::__unordered_map_hasher, il2cpp::metadata::SZArrayClassHash, true>, std::__1::__unordered_map_equal, il2cpp::metadata::SZArrayClassCompare, true>, std::__1::allocator > >::find(__k=0x00000000) at __hash_table:2020 frame #3: 0x01049608 mazeracing`il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(TypeInfo*, unsigned int, bool) [inlined] std::__1::unordered_map > >::find(this=, __k=0x00000000) at unordered_map:968 frame #4: 0x01049608 mazeracing`il2cpp::metadata::ArrayMetadata::GetBoundedArrayClass(elementClass=0x00000000, rank=1, bounded=false) + 168 at ArrayMetadata.cpp:459 frame #5: 0x01055648 mazeracing`il2cpp::vm::Array::New(elementTypeInfo=, length=) + 12 at Array.cpp:50 frame #6: 0x00968c0c mazeracing`ConvertContactToMono [inlined] CreateScriptingArray + 132 at ScriptingExportUtility.h:16 frame #7: 0x00968c08 mazeracing`ConvertContactToMono + 128 at MessageParameters.cpp:29 frame #8: 0x009aba96 mazeracing`HandleNotifications [inlined] Arguments + 248 at MonoBehaviour.cpp:1207 frame #9: 0x009ab99e mazeracing`HandleNotifications + 362 at MonoBehaviour.cpp:1276 frame #10: 0x00847b48 mazeracing`SendMessageAny [inlined] HandleMessage + 96 at MessageHandler.h:91 frame #11: 0x00847b3e mazeracing`SendMessageAny + 86 at GameObject.cpp:1185 frame #12: 0x00a849ee mazeracing`ProcessContacts [inlined] GetOtherRigidbodyOrCollider + 154 at GameObject.h:483 frame #13: 0x00a849d8 mazeracing`ProcessContacts + 132 at PhysicsManager.cpp:2435 frame #14: 0x00a83a42 mazeracing`ProcessRecordedReports + 30 at PhysicsManager.cpp:2489 frame #15: 0x00a8395a mazeracing`FixedUpdate + 1386 at PhysicsManager.cpp:1408 frame #16: 0x0096aa98 mazeracing`PlayerLoop + 896 at Player.cpp:1804 frame #17: 0x007b69d2 mazeracing`UnityPlayerLoopImpl + 14 at LibEntryPoint.mm:236 frame #18: 0x0005f7d8 mazeracing`UnityRepaint + 432 at UnityAppController+Rendering.mm:246 frame #19: 0x0005f590 mazeracing`__51-[UnityAppController(.block_descriptor=0x15d0d690) repaintDisplayLink]_block_invoke + 56 at UnityAppController+Rendering.mm:52 frame #20: 0x02f6019a libdispatch.dylib`dispatch_call_block_and_release + 10 frame #21: 0x02f60186 libdispatch.dylib`dispatch_client_callout + 22 frame #22: 0x02f63e9c libdispatch.dylib`dispatch_main_queue_callback_4CF + 1504 frame #23: 0x24b5b888 CoreFoundation`CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 8 frame #24: 0x24b59fa8 CoreFoundation`_CFRunLoopRun + 1512 frame #25: 0x24aa59a0 CoreFoundation`CFRunLoopRunSpecific + 476 frame #26: 0x24aa57b2 CoreFoundation`CFRunLoopRunInMode + 106 frame #27: 0x2c4671a8 GraphicsServices`GSEventRunModal + 136 frame #28: 0x28256634 UIKit`UIApplicationMain + 1440 frame #29: 0x000554c4 mazeracing`main(argc=, argv=) + 260 at main.mm:40 (lldb)
So it crashes if you comment out Handheld.Vibrate? If that isn't a typo then how could that function call be causing the crash if it still crashes when you remove it?
Is Wall set to something or is it null? $$anonymous$$y first assumption would be that it's null.

It wasn't a typo, but further testing revealed that it wasn't the reason.
Hi, have you tried to provide the full declaration for the OnCollisionEnter callback: void OnCollisionEnter(Collision collision). It might be an IL2CPP problem if the callback is not fully declared.
Also, you could add some "if ( Wall != null)" to be sure.
Another thing: using PlayerPref object during a collision is not a good practice: this function will go check the internal storage, this is very slow. I would store the result in a private variable that is initialized with calling playerpref only during Start, or making it a more global parameter for the whole game.
Yes it started crashing when I changed to IL2CPP. I'll try with full declaration.
Adding (Collision coll) solved the problem.
A huge thank you to both of you for your help. :)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can someone help me with the jumping in this Player Controller script? 1 Answer
OnTriggerEnter Not Working 1 Answer