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 stuey91 · Oct 03, 2017 at 01:04 AM · unity 5scripting problemtriggersmultiple objectsreference-other-object

OnTriggerEnter Script only working on duplicated object

I have one script that makes an object move from one target, then back to the first target. This script also has a bool that the second script accesses. The second script is the trigger, so when the player enters the trigger, the first scripts bool is true, and the object moves to point a, when the player leaves the trigger, the object moves back to point b.

public class Platform_Up : MonoBehaviour { public bool triggered; public Transform target_a; public Transform target_b; public float platform_speed;

 void Start()
 {
     //triggered = false;
 }

 void Update()
 {
     if(triggered)
     {
         float step = platform_speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target_a.position, step);
     }

     if(!triggered)
     {
         float step = platform_speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target_b.position, step);
     }
 }

}

The problem is when i duplicate the object, theres 2 triggers. No matter which objects trigger i enter, the duplicated object only moves. The same happens if i duplicate again, the third one only moves.

public class Platfor_Up_Trigger : MonoBehaviour {

 void OnTriggerStay2D(Collider2D other)
 {
     if(other.tag == "Player" || other.gameObject.tag == "Spawned Square")
     {
         GameObject platform = GameObject.Find("Platform_Moving_Up");
         Platform_Up platform_script = platform.GetComponent<Platform_Up>();
         platform_script.triggered = true;
     }
 }
 void OnTriggerExit2D(Collider2D other)
 {
     if (other.tag == "Player" || other.gameObject.tag == "Spawned Square")
     {
         GameObject platform = GameObject.Find("Platform_Moving_Up");
         Platform_Up platform_script = platform.GetComponent<Platform_Up>();
         platform_script.triggered = false;
     }
 }

}

I was wondering if there was a way to only have the object im triggering move, and not the rest of the duplicated objects

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 Dragate · Oct 04, 2017 at 01:03 PM

If I understood correctly, these two scripts are on the same gameobject. It seems to me that GameObject.Find("Platform_Moving_Up") from all duplicated objects get reference to the same platform, which is the only one working.

In Platfor_Up_Trigger class (both in triggerStay and triggerExit), could you delete

 GameObject platform = GameObject.Find("Platform_Moving_Up");

and change

 Platform_Up platform_script = platform.GetComponent<Platform_Up>();

to

 Platform_Up platform_script = gameObject.GetComponent<Platform_Up>();

? Let's see if that works out for you.

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 stuey91 · Oct 13, 2017 at 07:43 PM 0
Share

Hi sorry for the late reply and thanks for the help. I tried that but now its object not set to an instance of an object when the trigger is entered and exited.

avatar image Dragate stuey91 · Oct 16, 2017 at 06:51 AM 0
Share

If it can't find the "Platform_Up" script, then the 2 scripts(Platfor_Up_Trigger & Platform_Up) are not on the same gameObject? Could you clarify where each script is attached to exactly?

A guaranteed solution is to make platform_script a public variable in the Platfor_Up_Trigger class and assign manually (via Inspector) the platform you want to be referenced to each time you create a new platform.

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

184 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

Related Questions

How to make audioclip reference to another script 0 Answers

How to assign one script on different gameobject of 1 prefab and make them run one by one 0 Answers

Point Counter Works Only Once! 1 Answer

I have overrided a built-in script accidentally and not sure how to fix 2 Answers

The spaceship acceleration script is good ? And how to use it with engine ? 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