I'm using a pickup script from the third person sample pack that's on the unity's community site. I suck at javascript, and I'm trying to add a pickup type, but I just don't know where to even begin. Here's the code I'm working with: Code (csharp): enum PickupType { Health = 0, ExtraLife = 1 } var pickupType = PickupType.Health; var amount = 1; var sound : AudioClip; private var used = false; function ApplyPickup (health : ThirdPersonHealth) { if (pickupType == PickupType.Health) { if (health.health >= health.maxHealth) return false; health.AddHealth(amount); } else if (pickupType == PickupType.ExtraLife) { health.AddLife(amount); } return true; } function OnTriggerEnter (col : Collider) { var health : ThirdPersonHealth = col.GetComponent(ThirdPersonHealth); //* Make sure we are running into a player //* prevent picking up the trigger twice, because destruction // might be delayed until the animation has finnished if (used || health == null) return; if (!ApplyPickup (health)) return; used = true; // Play sound if (sound) AudioSource.PlayClipAtPoint(sound, transform.position); // If there is an animation attached. // Play it. if (animation animation.clip) { animation.Play(); Destroy(gameObject, animation.clip.length); } else { Destroy(gameObject); } } // Auto setup the pickup function Reset () { if (collider == null) gameObject.AddComponent(BoxCollider); collider.isTrigger = true; } @script AddComponentMenu("Third Person Props/Pickup") Any help is appreciated.
I modified it a bit, but what i have doesn't work. It's giving me an error saying "an instance of type 'ScoreGood' is required to access the non static member 'addtoscore.'" ScoreGood is a script I have that's just to add to the score variable, and addtoscore is the function. Here's the code: Code (csharp): enum PickupType { Health = 0, ExtraLife = 1, Score = 2 } var pickupType = PickupType.Health; var amount = 1; var sound : AudioClip; private var used = false; function ApplyPickup (health : ThirdPersonHealth) { if (pickupType == PickupType.Health) { if (health.health >= health.maxHealth) return false; health.AddHealth(amount); } else if (pickupType == PickupType.ExtraLife) { health.AddLife(amount); } else if (pickupType == PickupType.Score) { ScoreGood.AddToScore () ; } return true; } Everything afterwards is the exact same as the script from my first post. I just don't know why it's giving me this error, any ideas? Once again, any help is appreciated. Thanks[/code]
At the risk of basically repeating that message, you need an instance of 'ScoreGood' to access 'addtoscore'. Or to put it a slightly different way, you need a reference to 'ScoreGood' somehow. You can do it a number of ways; here's one: Code (csharp): var scoreScript = FindObjectOfType(ScoreGood); scoreScript.AddToScore () ; FindObjectOfType returns the first object it finds of that type; presumably you only have one instance of that script, so that's OK. --Eric