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 winonanassey · Nov 24, 2017 at 11:27 PM · instances

instances executing at the same time(solved)

I trigger a method on one instance of a script and all the instances of the script execute at once?

I use it on 4 objects here is the code

 using UnityEngine;
 
 public class Pikup : MonoBehaviour {
 
     public GameObject pikup;
     public Camera Plar;
     public GameObject pickupic;
     public Item itemtobepickupt;
 
     void Update()
     {
         RaycastHit hit;
         if (Physics.Raycast(Plar.transform.position, Plar.transform.forward, out hit))
         {
 
             Pikup pikup = hit.transform.GetComponent<Pikup>();
             if (pikup != null)
             {
                 pickupic.SetActive(true);
             }
             else
             {
                 pickupic.SetActive(false);
             }
 
             if (pikup != null && Input.GetButtonDown("E")) {
                 PickupItem();
 
             }
         }
     }
 
     void PickupItem ()
     {
        pickupic.SetActive(false);
        bool wasPickedUp = Invtorey.instancs.Add(itemtobepickupt); 
 
         if (wasPickedUp)
         Destroy(gameObject);
     }
 
 }

thanks

Comment
Add comment · Show 1
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 shakecaine · Nov 25, 2017 at 12:18 PM 0
Share

Please provide code and description where, how and on how many objects you use the script on.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shakecaine · Nov 26, 2017 at 10:57 AM

Of course they execute all at once. To start with please, please for the love of us all read through manual: https://docs.unity3d.com/Manual/index.html

Answer is very simple. You hit object with raycast, pikup is not null and you have E button clicked. Each of the objects that has the script does not care if it was the object that was hit, so all of them execute same code as all conditions are fulfilled. For all of the objects these conditions in if statement are true, so code executes.
If you want to detect collision i recommend using OnCollisionEnter instead as that code will only execute when something collides with your collider. For now you just send 4x rays each frame. Have raycasting on the player instead and handle colliders on objects.

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 winonanassey · Nov 26, 2017 at 08:12 PM 0
Share

Oh, why didn't i think of that, thanks

avatar image shakecaine winonanassey · Nov 27, 2017 at 12:05 AM 0
Share

If you can, please mark my answer as best answer to your question.

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

120 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

Related Questions

Is it possible to customize how a variable of a certain type would look in the inspector? 1 Answer

How to create a new instance of an object? 0 Answers

Different methods for instances of a ScriptableObject 0 Answers

Accessing decal's material's parameters in animation window. 0 Answers

UI button click to trigger functions in multiple instances 0 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