Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PrezThompkins · Jun 28, 2011 at 11:45 PM · referenceinventoryreference-other-object

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);
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PrezThompkins · Jun 28, 2011 at 11:51 PM 0
Share

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.

avatar image testure · Jun 28, 2011 at 11:55 PM 1
Share

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.

avatar image PrezThompkins · Jun 28, 2011 at 11:57 PM 0
Share

... 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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Referencing another C# Script Error 1 Answer

Can't re-enable a gameobject 1 Answer

How do I find which objects are referencing another? 10 Answers

Setting Inventory Reference to a Object For Pickup when the Inventory is a Instantiated Prefab 1 Answer

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges