totally confused with GetComponent

sorry guys I am a total idiot... but if i wanna make it so that when object registers a collision, it changes the value of a public variable "HitPoints" on a script attached to another object called "Health"... how can I do that? likewise... could I use this the same way to change to "Use Gravity" variable from true to false on an attached rigidbody instead of changing the hitpoints on the health script?

I take it you're having trouble getting this to work. Can you edit your post and show us the code that you have currently?

Well I don't really have anything because I'm not sure how to use it...

1 Answer

1

Yes you can.

You need to instantiate the class you keep the public variable "health" into an object and store it into where you need it, like this:

public class MyHealthClass : MonoBehaviour
{
   public float Health;

   void Start()
   {
      Health = 100;
   }

   void Update()
   {
       //Nothing here, boring script waiting for work
   }

   //Adds value to current health
   public void ChangeHP(float changeAmount)
   {
      Health += changeAmount;
   }
}

public class myBoxThingyThatCanCollide : MonoBehaviour
{
    MyHealthClass myObjectReference;

    void Start()
    {
        myObjectReference = new MyHealthClass();
    }

    void Update()
    {

    }

    //Requires a characterController
    void OnControllerColliderHit(ControllerColliderHit cch)
    {
        //Subtracts 10 life on collision with anything
        myObjectReference.changeHP(-10);
    }
}

I didn't check a compile so please forgive any syntax error, the most of it should be ok

I only know javascript and i'm either totally stupid or this isn't javascript sorry...

This is C# indeed. I had no idea you didn't know C# sorry, guess it's a good idea to post that in questions :) I don't know JS that much either so I can't really convert it, I can just convert JS --> C#

its cool ill just re-ask it