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 /
  • Help Room /
avatar image
0
Question by robertbalassan · Sep 13, 2018 at 09:39 AM · scripting probleminspector

How to make the same script on multiple objects work just for the object where it is called?

My problem is really simple but I do not know how to search for this and actually find a good topic, so I am sorry if this is a double post.

I have a scene with multiple identical levers which all use one script:

 using UnityEngine;
 
 public class LeverScript : MonoBehaviour {
 
     public GameObject objectToUse;
     
     public void UseObject()
     {
         Debug.Log("Using lever!");        
         Destroy(objectToUse);
     }
 }

I also have an USE button on my canvas which calls my UseObject() from the lever. This is supposed to deactivate a forcefield.

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class JoyButtonUse : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
 {     
     public void OnPointerDown(PointerEventData eventData)
     {
         Debug.Log("Joybutton Used");
         FindObjectOfType<LeverScript>().UseObject();  
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         throw new System.NotImplementedException();
     }
 }

The problem is that even if in interface there are different forcefields attached to each lever as objectToUse, they all call just one and it is really impossible for me to understand why.

Update: I tried to change my code as follows: The lever script:

 using UnityEngine;
 
 public class LeverScript : MonoBehaviour {
 
     public GameObject objectToUse;
 
     private void Update()
     {
         if (JoyButtonUse.usePressed)
         {
             UseObject();
         }
     }
 
     public void UseObject()
     {
         Debug.Log("Using lever!");        
         Destroy(objectToUse);
     }
 }

and the use button script to this:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class JoyButtonUse : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
 {
     public static bool usePressed = false;
 
     public void OnPointerDown(PointerEventData eventData)
     {
         usePressed = true;
         Debug.Log("Joybutton Used: " + usePressed);       
         
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         usePressed = false;
         Debug.Log("Joybutton released: Pressed is: " + usePressed);
     }
 }

 public void UseObject()
 {
     Debug.Log("Using lever!");        
     Destroy(objectToUse);
 }}


so that my button does not know about the in game objects.

But now, all objects are destroyed when using any lever.

How can I make those scripts work independently while being basically the same? Or is any better optimal solution for my needs?

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
1
Best Answer

Answer by JVene · Sep 13, 2018 at 07:41 PM

You second code is fashioned to destroy all objects, so it may be a small comfort to know the computer is doing exactly what you've told it to do (that's a bad attempt at humor). The trick, now, is to figure out how to reference only one of the instances of LeverScript.


The opposite is true of the first code posted, which uses FindObjectOfType, which, as the name implies, finds one object and only calls UseObject on that one that's found, which will be the first one this 'find' call locates, and usually one you might not intend.


So which one? In the second version, Update in LeverScript is being processed at every Update cycle for all scripts that have an Update method. As a result, they are all 'seeing' the same static JoyButton.usePressed, and when true, they are all calling UseObject. How would you identify only one of the several LeverScript based objects to be called? This is key. In what way can you determine the one lever you intend for the call to UseObject, and not any other LeverScript object?


I see nothing in your post that helps identify which one. I have to guess to proceed.


Let's say something happened in the game. Maybe an object contacted the lever to be moved, and so there might be some collision detected. THAT would identify one, and only one of the several levers to be 'used'.


Maybe there's something that moves focus from one lever to another. How would you select a particular lever? Maybe instead of clicking a button, you need to click the lever itself?


Your first approach is actually closer, but....maybe you need to use FindObjectsOfTypeAll, which gives you a list of all the lever scripts, and then search through that list of the one and only one you intend to call UseObject.


Comment
Add comment · Show 2 · 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 robertbalassan · Sep 13, 2018 at 09:54 PM -1
Share

you explained it perfectly, i understand now! thanks!

avatar image tormentoarmagedoom · Sep 14, 2018 at 08:40 AM 0
Share

Good answer JVene !:D

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

224 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom inspector for items in array. 1 Answer

Animation Ignores Script 2 Answers

How to reference custom assets from Inspector or Script 0 Answers

GameObject array not showing on the inspector 0 Answers

Why is Unity showing the wrong variables in the Inspector? 1 Answer


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