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 SBull · Sep 30, 2013 at 05:01 PM · javascriptgameobjectprefabvariables

How would I change the value of one prefab's variable via script without changing all of them?

I am making a game using javascript where several characters are moving around a level on their own. The player starts out with one character. After they tell that character to begin, the game creates the next character for the player to send into the level by creating a prefab of the first one (which means that it has the same script attached to it as the original character). This works fine in some cases and I can get several characters into the level, but when one of them hits a trigger which changes one of it's variables, it changes the variable on all the other prefabs as well. How can I make it so that each prefab acts completely independently of the others.

If that wasn't very clear I'll try giving this example: The player clicks on the first character. The first character begins walking and the second character is spawned. The player clicks on the second character. The second character begins walking and the third character is spawned. By this point, the first character has reached a trigger which is supposed to act as a teleporter and move them to a specific position. However, all the created characters are moved to that position instead of just the first character.

Thanks for any help.

Comment
Add comment · Show 3
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 robertbu · Sep 30, 2013 at 05:01 PM 0
Share

This would happen if you are using static variables, and there might be other causes. Please post your code.

avatar image SBull · Sep 30, 2013 at 07:19 PM 0
Share

Okay. So here's an example of something that works the way it's supposed to and one that doesn't. ($$anonymous$$y characters are squirrels by the way.)

 var squirrel : GameObject;
 
     function OnTriggerEnter (theCollision : Collider)
     {
             if(theCollision.gameObject.tag == "treeTrigger")
             {
                 isClimbing = true;
             }
         
             else if(theCollision.gameObject.tag == "homeEntrance")
             {            
                 //$$anonymous$$oves squirrel to top of tree
                 yield WaitForSeconds(1.0);
                 squirrel.transform.position = homeExit.transform.position;
             }
     }

If I have multiple squirrels in play and one touches the treeTrigger, it changes the isClimbing variable to true for just the squirrel that touched the treeTrigger. But if I have multiple squirrels in play and one touches the homeEntrance trigger, all of the squirrels in play will be teleported to the location of the homeExit. I'm guessing this has to do with me referencing the squirrel GameObject variable before transform.position but I don't know how to change it's location without telling it what I want to teleport.

avatar image robertbu · Sep 30, 2013 at 09:46 PM 0
Share

There's nothing wrong with this code that I can see. Something else is going on. Do you have a sample project you can post?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TrickyHandz · Oct 01, 2013 at 03:22 AM

There is a chance that the "squirrel" variable is always being set to the first "squirrel" game object. I would have to see the rest of you code to see if that is the case. However, rather than referencing a variable, just call transform directly so that the object is always calling its own transform and there is no chance for confusion:

 function OnTriggerEnter (theCollision : Collider)
 {
     if(theCollision.gameObject.tag == "treeTrigger")
     {
         isClimbing = true;
     }
  
     else if(theCollision.gameObject.tag == "homeEntrance")
     {       
         //Moves squirrel to top of tree
         yield WaitForSeconds(1.0);

         // Change this gameObject's transform.position to
         // the same as homeExit.transform.position
         transform.position = homeExit.transform.position;
     }
 }
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 SBull · Oct 01, 2013 at 04:23 PM 0
Share

Yep. That did it. I thought you had to declare the GameObject you wanted to transform. Now that I know not adding the GameObject will only change the status of the object the script is connected to it should make things much easier. Thank you.

avatar image TrickyHandz · Oct 01, 2013 at 04:28 PM 0
Share

Glad I could help. Happy coding!

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

15 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

Related Questions

Load Prefabs in Array with Javascript 1 Answer

Does enabling/disabling Scripts have a high performance cost? 1 Answer

Bullet Instantiation 1 Answer

What variables can i declare? 1 Answer

Assigning variables to GameObjects 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