- Home /
Dictionary - Works in Editor, and in Dev Build, but not in Release Build?
I'm using this code in my game to multiply the damage generated by impacts depending on the sharpness values attributed to physics materials:
using UnityEngine;
using System.Collections.Generic;
public class MaterialSharpnessDefiner : MonoBehaviour {
[System.Serializable]
public class MaterialSharpnessPair : System.Object {
public PhysicMaterial material;
public float sharpness = 0;
}
public MaterialSharpnessPair[] materialSharpnessPairs;
// Use this for initialization
void Start () {
foreach(MaterialSharpnessPair msp in materialSharpnessPairs)
{
MaterialSharpness.materialSharpnessDictionary.Add(msp.material, msp.sharpness);
}
}
}
public static class MaterialSharpness {
public static Dictionary<PhysicMaterial,float> materialSharpnessDictionary = new Dictionary<PhysicMaterial,float>();
public static float GetSharpnessMultiplier(this Collider collider)
{
return collider.sharedMaterial.GetSharpnessMultiplier();
}
public static float GetSharpnessMultiplier(this PhysicMaterial material)
{
if(materialSharpnessDictionary.ContainsKey(material))
{
return 1 + materialSharpnessDictionary[material];
}
else
{
return 1;
}
}
}
This is the code that detects collisions and deals out damage points:
using UnityEngine;
using System.Collections;
public class ImpactPain : MonoBehaviour {
public bool painCollisionEnter = true;
public bool painCollisionStay = false;
public float stayTime = 1.0f;
public float frictionMultiplier = 0;
public float impactMultiplier = 1;
public float minImpactForce = 5;
public float minFrictionForce = 5;
public PsycheSimple psycheSimple;
public ParticleEmissionScaler particleEffect;
public AnimationCurve painForEmission;
void OnCollisionStay(Collision collision)
{
if(painCollisionStay)
{
OnPainCollision(collision, Time.deltaTime / stayTime);
}
}
void OnCollisionEnter(Collision collision)
{
if(painCollisionEnter)
{
OnPainCollision(collision, 1.0f);
}
}
void OnPainCollision(Collision collision, float multiplier)
{
float sharpness = collision.collider.GetSharpnessMultiplier();
float pain = Mathf.Max(0f, (collision.impactForceSum .magnitude * sharpness) - minImpactForce ) * impactMultiplier +
Mathf.Max(0f, (collision.frictionForceSum.magnitude * sharpness) - minFrictionForce) * frictionMultiplier;
psycheSimple.painAdd += pain * multiplier;
if(particleEffect != null && collision.contacts.Length > 0)
{
Vector3 averagePosition = Vector3.zero;
Vector3 normal = Vector3.zero;
foreach(ContactPoint contact in collision.contacts){
averagePosition += contact.point;
normal += contact.normal;
}
averagePosition /= (float)collision.contacts.Length;
normal.Normalize();
float scaler = painForEmission.Evaluate(pain * multiplier);
if(scaler > 0.01f)
{
ParticleEmissionScaler newParticleEffect = ParticleEmissionScaler.Instantiate(particleEffect,
(averagePosition + transform.position * 2f) / 3f, Quaternion.LookRotation(-normal)) as ParticleEmissionScaler;
if(newParticleEffect) {
newParticleEffect.transform.parent = transform;
newParticleEffect.scaler = painForEmission.Evaluate(pain * multiplier);
}
newParticleEffect = ParticleEmissionScaler.Instantiate(particleEffect,
averagePosition, Quaternion.LookRotation(normal)) as ParticleEmissionScaler;
if(newParticleEffect) {
newParticleEffect.transform.parent = collision.transform;
newParticleEffect.scaler = painForEmission.Evaluate(pain * multiplier);
}
}
}
}
}
The strange thing is, this code seems to work perfectly fine in the editor. However, in the release version, the effect of impact damage is only sparsely applied or is not applied at all to some rigidbodies. When I comment out "float sharpness = collision.collider.GetSharpnessMultiplier();" and set sharpness to 1 instead, it seems to apply damage consistently albeit with the wrong amount.
Why is this happening? Please reply soon. :)
-Steve
Your answer
![](https://koobas.hobune.stream/wayback/20220613081151im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
static objects in android build 0 Answers
How to Instantiate objects while in editor? 1 Answer
Unserialized private variable values from static instance persist when exiting play mode 1 Answer
does the static keyword work on generic dictionary 2 Answers
Lighting differences between scenes in Player but not in Editor 2 Answers