Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 rich_seabirdsim · Sep 26, 2014 at 02:43 AM · gameobject.findgameobject

All instances of a prefab effected by single access -- no Static variables

I'm really at my wits end with this. Sunk a bunch of time into figuring out how to access a variable attached to an object that was a child of a child. I've got some treasure chests that are supposed to open when I 'ping' them from a second script. I figured out how to access the script, but now all of the treasure chests open with one click which I don't want. I have not declared any static variables, but its as though I'm using some kind of implicit static declaration. I can't find anything unusual assigned in the inspector. Here's the code from the animation script:

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(Animator))]
 //[RequireComponent(typeof(SonarcursorScript))]
 
 public class TreasureControl : MonoBehaviour {
 
     public SonarcursorScript sonarcursorScript;
     
     public GameObject TreasureLight;
 
     Animator anim;
 
     // Use this for initialization
     void Start () {
         
         anim = GetComponent<Animator>();
         anim.SetBool ("open", false);
         TreasureLight.renderer.enabled=false;
         GameObject sub = GameObject.Find ("Sub");
         sonarcursorScript = sub.GetComponentInChildren<SonarcursorScript>();
             
     }
     
     // Update is called once per frame
     void Update () {
 
         if (sonarcursorScript.boxInt == 21)
  
         {
             anim.SetBool ("open", true);
 //            Debug.Log ("This is it  "+sonarcursorScript.boxInt);
             TreasureLight.renderer.enabled=true;
             
         }
     }
 }

and relevant code from the calling script:

 using UnityEngine;
 using System.Collections;
 
 public class SonarcursorScript : MonoBehaviour {
 
     public int boxInt = 0;
     public string sonrTarget = "helpme";
 
 void Update() {
 if(sonrTarget == "Box") 
 boxInt =21;
 
 }

When boxInt is passed as 21 to TreasureControl.cs, all my boxes animate at once. They were all instances of the same prefab. Breaking the prefab instance did not fix it. Changing the instantiation method didn't either. I'd appreciate any help. Thanks!

Comment
Add comment · Show 2
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 rich_seabirdsim · Sep 27, 2014 at 03:50 AM 0
Share

Follow-up. I created a new treasureControl2.cs script as well as a new animator (both renamed), attached them to one of the chests, and they all still opened at once - even with different/multiple scripts and controllers .

avatar image 767_2 · Sep 27, 2014 at 11:22 AM 0
Share

how you distinguish between what you clicked , how you check which object you clicked

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Sep 27, 2014 at 03:44 PM

The problem is how you have things structured, but since I don't know your hierarchy, I'm unsure of the fix. These two lines:

  GameObject sub = GameObject.Find ("Sub");
  sonarcursorScript = sub.GetComponentInChildren<SonarcursorScript>();

...will always return the same game object, and therefore the same component.

Comment
Add comment · 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

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

Get list of gameObjects and change booleans within them 2 Answers

GameObject.FindGameObjectWithTag 3 Answers

Find a GameObject without a reference 2 Answers

C# to find a Object in a direction on a 2D map 0 Answers

Remove all objects that a certain type 2 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