- Home /
Changing the Variable of an objects script, through a different objects event.
Hey guys, this is my first post on Unity Answers!
I'm having a little bit a trouble, on something I'm sure should be simple.
I have an Object (ObtainableObject) that should be picked up by the player, and then removed from the game world. When the player move's into it's range, they are prompted to push the "pick up object" key, which will add +1 to the players Inventory Script value of "PickupObjectsHeld"... However, I'm having trouble figuring out exactly how to reach that result... I'v looked through several help threads, and I'v gotten closer to the answer, but not quite there yet.. Here's my code for the Obtainable Object Class!
using UnityEngine;
using System.Collections;
public class ObtainableObjectScript : MonoBehaviour {
// Call from the Hierarchy
public GameObject thePlayer;
public Vector3 DistanceToPlayer;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
public void Update()
{
GameObject thePlayer = GameObject.Find("ThePlayer");
DistanceToPlayer = thePlayer.transform.position - transform.position;
if (DistanceToPlayer.magnitude < 10)
{
print("Pick Up");
if (Input.GetButtonDown("Use/Pickup"))
{
gameObject.Find("ThePlayer").GetComponent("ThePlayerInventory").pickupObjectsHeld+= 1;
Destroy(this);
}
}
}
}
Answer by testure · Jun 28, 2011 at 11:49 PM
If your obtainable object is something you would bump into to collect, you could simply add a collider/trigger to it, and OnTriggerEnter (or collider if thats what you're doing), do something like so:
ObtainableObjectScript otherObject = collisionInfo.collider.transform.GetComponent<ObtainableObjectScript>();
that will give you a reference to the object that you touched, and from there you can do whatever you want with it.
for further reading:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html
That's originally what I was doing, and it is a good suggestion... However, I changed it to involve a range... So, if the player is standing withing a certain range of it, it'll prompt to pick up, and wont do so until the player pushes to "grab object" key... If all else fails, I could go back to the collider however :P.
well you can do the same thing with a distance based solution- just put a trigger on your player and make it a rigidbody set to kinematic (so it doesn't reorder the scene graph while it moves). Then you can use OnTriggerEnter and OnTriggerExit to track objects of interest that are within a particular range (the radius of your trigger).
I use a system like that for my target tracking systems in my game, if an enemy enters the trigger, it goes into a list of transforms that I can cycle through as a potential target.
... You sir, are a genius lol. Thanks, This will really help. That sounds a lot more modular then how I was doing things as well!