using UnityEngine;
using InsaneSystems.HealthbarsKit
namespace InsaneSystems.HealthbarsKit
{
/// <summary> Simple auto-damage component for example scene. Does periodic damage to any object which have this component and Damageable component.</summary>
public class DamagingCollision : MonoBehaviour
{
[SerializeField] float damageValue = 5f;
Damageable damageable;
float damage;
private void oncollisionenter(DamagingCollision)
{
damageable = GetComponent<Damageable>();
}
void Update()
{
if DamagingCollision;
damageable.TakeDamage(damageValue);
}
}
}
This is certainly not the correct way to define this function:
Go check the docs for the correct spelling.
Also, what is this text noise??!!!
That absolutely can’t be correct!
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
im still learning to code i tried modifying the damaging script from timer base to collision base there isnt a tutorial for this asest im afraid i do not understand
- Start with Unity Learn > Pathways.
- There is a term called Case Sensitivity. C# is case-sensitive language.
OnCollisionEnter
function must be written like docs says:
void OnCollisionEnter(Collision collision)
{
}
- I assume that you use a text editor to edit scripts. This is wrong. Use a Code Editor like Visual Studio Code. It will indicate you on errors.
if else
statement must look like this:
if (damage < 20f)
{
// your code
}
else
{
// your another code
}
you dont know enough about scripting to be modifying assets
you should start from the ground up, start learning script without messing around with something complicated like trying to mess around with a random asset you downloaded and all the baggage that comes with it
Also, being as this is 2D physics, you should be using the 2D physics callbacks like this: Unity - Scripting API: Collider2D.OnCollisionEnter2D(Collision2D)
I think the sentiment above is essentially, trying to run before you’ve learned to walk is a frustrating way to move forward. As much as I know it can feel tedious, I would absolutely spend a little more time on the basics of C# first.
public class DamagingCollision : MonoBehaviour
{
[SerializeField] float damageValue = 5f;
private void OnCollisionEnter(Collision collision)
{
Damageable damageable = collision.gameObject.GetComponent<Damageable>();
if (damageable != null)
{
damageable.TakeDamage(damageValue);
Debug.Log($"Dealt {damageValue} damage to {collision.gameObject.name}");
}
}
}
To note as above, the OP is asking about 2D physics.
Oh then yes, you definitely have failed to do Step #2 as I posted above.
You can’t meaningfully modify stuff unless you understand it.
And you certainly cannot program without getting 100% of your typing correct.
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
thank you all for the replies i am still learning
so how do i get this script to interact with this one ?
void OnHealthChanged() => HealthWasChanged?.Invoke(health);
public void TakeDamage(float damage)
{
health = Mathf.Clamp(health - damage, 0, maxHealth);
OnHealthChanged();
if (health == 0)
Die();
}
public void Die() => Destroy(gameObject);
could i run an void update to detect if its hit?