Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dublinjonny · Feb 12, 2014 at 01:56 PM · c#objectsmultipleone

Why does one script not work on multiple gameobjects?

I have applied a C# script to a game object that enables / disables other game objects as they enter its trigger and it works fine. However when i copy and paste that game object or rebuild it so there is now two of the trigger objects and apply the same script to the second trigger object it registers true on the triggers but it wont run enable / disable the other game objects although the first game object will remain working just fine .

If I disable the original game object the copy works just fine?

 using UnityEngine;
 using System.Collections;
 
 public class RingHook : MonoBehaviour {
 
 
 
 
     public bool ringOneTrigger ;
     public bool ringTwoTrigger ;
     public bool ringThreeTrigger ;
     public bool ringFourTrigger ;
     public bool ringFiveTrigger ;
     public bool ringSixTrigger ;
     public bool ringSixTriggerB ;
     public bool ringSevenTrigger ;
     public bool ringEightTrigger ;
     public bool ringNineTrigger ;
     public bool ringTenTrigger ;
 
     public GameObject ring1;
     public GameObject ring1BoxCollider;
     public GameObject ring1SphereCollider;
 
     public GameObject ring2;
     public GameObject ring2BoxCollider;
     public GameObject ring2SphereCollider;
 
     public GameObject ring3;
     public GameObject ring3BoxCollider;
     public GameObject ring3SphereCollider;
 
     public GameObject ring4;
     public GameObject ring4BoxCollider;
     public GameObject ring4SphereCollider;
 
     public GameObject ring5;
     public GameObject ring5BoxCollider;
     public GameObject ring5SphereCollider;
 
     public GameObject ring6;
     public GameObject ring6BoxCollider;
     public GameObject ring6SphereCollider;
 
     public GameObject ring7;
     public GameObject ring7BoxCollider;
     public GameObject ring7SphereCollider;
 
     public GameObject ring8;
     public GameObject ring8BoxCollider;
     public GameObject ring8SphereCollider;
 
     public GameObject ring9;
     public GameObject ring9BoxCollider;
     public GameObject ring9SphereCollider;
 
     public GameObject ring10;
     public GameObject ring10BoxCollider;
     public GameObject ring10SphereCollider;
 
     public GameObject lowHookTrigger;
     public GameObject highHookTrigger;
 
     
 
     // Update is called once per frame
     void Awake () {
 
                       ring1 = GameObject.FindWithTag ("Ring1");
            ring1BoxCollider = GameObject.FindWithTag ("Ring1BoxCollider");
         ring1SphereCollider = GameObject.FindWithTag ("Ring1SphereCollider");
 
                       ring2 = GameObject.FindWithTag ("Ring2");
            ring2BoxCollider = GameObject.FindWithTag ("Ring2BoxCollider");
         ring2SphereCollider = GameObject.FindWithTag ("Ring2SphereCollider");
 
                       ring3 = GameObject.FindWithTag ("Ring3");
            ring3BoxCollider = GameObject.FindWithTag ("Ring3BoxCollider");
         ring3SphereCollider = GameObject.FindWithTag ("Ring3SphereCollider");
 
                       ring4 = GameObject.FindWithTag ("Ring4");
            ring4BoxCollider = GameObject.FindWithTag ("Ring4BoxCollider");
         ring4SphereCollider = GameObject.FindWithTag ("Ring4SphereCollider");
 
                       ring5 = GameObject.FindWithTag ("Ring5");
            ring5BoxCollider = GameObject.FindWithTag ("Ring5BoxCollider");
         ring5SphereCollider = GameObject.FindWithTag ("Ring5SphereCollider");
 
                       ring6 = GameObject.FindWithTag ("Ring6");
            ring6BoxCollider = GameObject.FindWithTag ("Ring6BoxCollider");
         ring6SphereCollider = GameObject.FindWithTag ("Ring6SphereCollider");
 
                       ring7 = GameObject.FindWithTag ("Ring7");
            ring7BoxCollider = GameObject.FindWithTag ("Ring7BoxCollider");
         ring7SphereCollider = GameObject.FindWithTag ("Ring7SphereCollider");
 
                       ring8 = GameObject.FindWithTag ("Ring8");
            ring8BoxCollider = GameObject.FindWithTag ("Ring8BoxCollider");
         ring8SphereCollider = GameObject.FindWithTag ("Ring8SphereCollider");
 
                       ring9 = GameObject.FindWithTag ("Ring9");
            ring9BoxCollider = GameObject.FindWithTag ("Ring9BoxCollider");
         ring9SphereCollider = GameObject.FindWithTag ("Ring9SphereCollider");
 
                      ring10 = GameObject.FindWithTag ("Ring10");
           ring10BoxCollider = GameObject.FindWithTag ("Ring10BoxCollider");
        ring10SphereCollider = GameObject.FindWithTag ("Ring10SphereCollider");
     
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.tag == "Ring1") 
             
         {
             ringOneTrigger = true;
         }
         
         if (other.tag == "Ring2") 
             
         {
             ringTwoTrigger  = true;
         }
         
         if (other.tag == "Ring3") 
             
         {
             ringThreeTrigger  = true;
         }
         
         if (other.tag == "Ring4") 
             
         {
             ringFourTrigger  = true;
         }
         
         if (other.tag == "Ring5") 
             
         {
             ringFiveTrigger = true;
         }
         
         if (other.tag == "Ring6") 
             
         {
             ringSixTrigger = true;
 
         }
         
         if (other.tag == "Ring7") 
             
         {
             ringSevenTrigger = true;
         }
         if (other.tag == "Ring8") 
             
         {
             ringEightTrigger = true;
         }
         if (other.tag == "Ring9") 
             
         {
             ringNineTrigger = true;
         }
         if (other.tag == "Ring10") 
             
         {
             ringTenTrigger = true;
         }
     }
     
     void OnTriggerExit (Collider other)
     {
         if (other.tag == "Ring1")
         {
             ringOneTrigger = false;
         }
         
         if (other.tag == "Ring2")
         {
             ringTwoTrigger  = false;
         }
         
         if (other.tag == "Ring3") 
             
         {
             ringThreeTrigger  = false;
         }
         
         if (other.tag == "Ring4") 
             
         {
             ringFourTrigger  = false;
         }
         if (other.tag == "Ring5") 
             
         {
             ringFiveTrigger = false;
         }
         
         if (other.tag == "Ring6") 
             
         {
             ringSixTrigger = false;
         }
         
         if (other.tag == "Ring7") 
             
         {
             ringSevenTrigger = false;
         }
         if (other.tag == "Ring8") 
             
         {
             ringEightTrigger = false;
         }
         if (other.tag == "Ring9") 
             
         {
             ringNineTrigger = false;
         }
         if (other.tag == "Ring10") 
             
         {
             ringTenTrigger = false;
         }
         
     }
 
     void Update()
     {
         if (ringOneTrigger == true)
         {
             ring1BoxCollider.SetActive (true);
             ring1SphereCollider.SetActive (false);
         } 
         else 
         {
             ring1BoxCollider.SetActive (false);
             ring1SphereCollider.SetActive (true);
         }
         if  (ringTwoTrigger == true)
         {
             ring2BoxCollider.SetActive (true);
             ring2SphereCollider.SetActive (false);
         } 
         else 
         {
             ring2BoxCollider.SetActive (false);
             ring2SphereCollider.SetActive (true);
         }
         
         if  (ringThreeTrigger == true)
         {
             ring3BoxCollider.SetActive (true);
             ring3SphereCollider.SetActive (false);
         } 
         else 
         {
             ring3BoxCollider.SetActive (false);
             ring3SphereCollider.SetActive (true);
         }
         
         if  (ringFourTrigger == true)
         {
             ring4BoxCollider.SetActive (true);
             ring4SphereCollider.SetActive (false);
         } 
         else 
         {
             ring4BoxCollider.SetActive (false);
             ring4SphereCollider.SetActive (true);
         }
         
         if  (ringFiveTrigger == true)
         {
             ring5BoxCollider.SetActive (true);
             ring5SphereCollider.SetActive (false);
         } 
         else 
         {
             ring5BoxCollider.SetActive (false);
             ring5SphereCollider.SetActive (true);
         }
         
         if (ringSixTrigger == true) 
         {
         ring6BoxCollider.SetActive (true);
         ring6SphereCollider.SetActive (false);
         } 
            else 
            {
            ring6BoxCollider.SetActive (false);
            ring6SphereCollider.SetActive (true);
            }
 
 
         if  (ringSevenTrigger == true)
         {
             ring7BoxCollider.SetActive (true);
             ring7SphereCollider.SetActive (false);
         } 
         else 
         {
             ring7BoxCollider.SetActive (false);
             ring7SphereCollider.SetActive (true);
         }
         if  (ringEightTrigger == true)
         {
             ring8BoxCollider.SetActive (true);
             ring8SphereCollider.SetActive (false);
         } 
         else 
         {
             ring8BoxCollider.SetActive (false);
             ring8SphereCollider.SetActive (true);
         }
         if  (ringNineTrigger == true)
         {
             ring9BoxCollider.SetActive (true);
             ring9SphereCollider.SetActive (false);
         } 
         else 
         {
             ring9BoxCollider.SetActive (false);
             ring9SphereCollider.SetActive (true);
         }
         if  (ringTenTrigger == true)
         {
             ring10BoxCollider.SetActive (true);
             ring10SphereCollider.SetActive (false);
         } 
         else 
         {
             ring10BoxCollider.SetActive (false);
             ring10SphereCollider.SetActive (true);
         }
 
     }
 
 }
 
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 sumeetkhobare · Feb 12, 2014 at 04:29 PM 0
Share

What's the name of the GameObject you attached this script? And what's the name of the copy of your gameObject?

I strongly feel that when you find the GameObjects with those Tags you have, may be, because of the same name or tag, its always returning only one gameobject, and not the other, so only that gameobject is disabled everytime.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gamescorpion · Oct 19, 2014 at 03:08 AM

I solved this by simply removing my script from the object and RE-adding it back in through the inspector. Worked fine for me afterwards. Its like when you duplicate objects, it keeps them with their original settings like a prefab, so I have to always remove/reapply items manually. Ex. Take a plane, add a texture, press CTRL+D to duplicate. After if you try and change the texture via inspector, BOTH copies will change the same way like a prefab would, HOWEVER if you now drag and drop a new texture from Project Explorer to one of the planes, the texture will change correctly and both objects will act the way you want. The same method will hold true with scripts and other items as per my findings.

Hope this helps others (I'm sure this is not the "Best" method, but hey it works lol!).

Comment
Add comment · Show 1 · 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 ireth92 · Mar 10 at 09:56 PM 0
Share

Wow, just wow... I removed the script and added it again as you suggested and it miraculously works now. I spent one hour trying to figure out what was wrong... turns out nothing was. Thanks a lot, Unity! Can't believe this is an issue since 2014.

avatar image
0

Answer by fox2402 · Feb 12, 2014 at 02:37 PM

Since you haven't post your code, i can't say a lot. But, if you want to make multiple object, try using a prefab and the Instantiate function, or else try to check your conditions inside your trigger. Can't say more without your code^^'

Comment
Add comment · Show 1 · 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 Dublinjonny · Feb 12, 2014 at 03:20 PM 0
Share

sorry ive updated it

avatar image
0

Answer by RedDevil · Feb 12, 2014 at 03:22 PM

When you disable objects this works.When you try to reenable objects this does not work since you disabled the object you disabled the script so now you cannot enable it again.

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 Dublinjonny · Feb 12, 2014 at 03:55 PM 0
Share

The script is not attached to those objects they enable and disable perfectly fine when I only use one trigger object , its when I try add the script to an identical object the trigger is recagnoised but it will not run the functions on the new trigger the origial trigger will continue to work

avatar image Dublinjonny · Feb 12, 2014 at 04:22 PM 0
Share

Ok so to solve this the script was fine , i added a second collider to the orignal trigger object and moved it into the position where I wanted the second and all is working fine now thanks guys

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

22 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

Related Questions

Making a simple C# script to change 3D text 1 Answer

Multiple Cars not working 1 Answer

How to get script components in multiple gameobjects C# 2 Answers

Trying to understand fingerId's - android c# 0 Answers

How to change the color of all objects that use the same material? 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