• 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
1
Question by MrAdventure · Mar 27, 2014 at 11:01 PM · variableprefab-instance

Script changing variable on prefab in Project, not Heirarchy

Hi there,

This is a new weirdness for me. I have a Prefab with a public variable called quantity. That prefab was dragged into the scene. The quantity variable should be decremented when a button is pressed.

It is the original prefab, in the Project window, that is changed instead. When I create two objects from the prefab, they are sharing the same variable. So I have two questions.

1) I thought dragging a Prefab into the Heirarchy created different instances of the object. Why are they sharing variables, how do I make them unique?

2) This value also stays changed on the original Prefab when exiting Play Mode. Up to this point, nothing has persisted when exiting Play Mode. What's going on there? (I haven't set it to save anything between plays.)

Thanks,

Victor

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 Ed unity · Mar 28, 2014 at 06:37 PM 0
Share

It sounds to me like when you change the variables in the script, you are not using this.transform or this.myvariable, but rather finding the prefab and then changing it's variables, thus changing all in game prefabs at the same time.

avatar image Leuthil · Mar 28, 2014 at 06:46 PM 0
Share

Is your variable set to static or something?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MrAdventure · Mar 29, 2014 at 09:02 AM

Thanks for answering!

Variable is public, not static.

I have set the value using this.quantity, and quantity. This.transform.quantity returns an error.

Even if I am finding the Prefab on accident, why is the change staying after I leave Play mode?

 void WhereToUse(Tap tap){

     if (dfInputManager.ControlUnderMouse == null)
     {

         Debug.Log("Plant is " + plant.name);

         Debug.Log("Plant here");

         Debug.Log("Tap Pos = " + tap.pos);

         Ray ray = Camera.main.ScreenPointToRay(tap.pos);

         RaycastHit hit;

         if(Physics.Raycast(ray, out hit) && plant != null) {

             var randomRotation = Quaternion.Euler( 0 , Random.Range (0, 360), 0);
         
             GameObject newPlant = Instantiate(plant, hit.point, randomRotation) as GameObject;

             Debug.Log("newPlant name = " + newPlant.name);

             Plant thePlant = newPlant.GetComponent<Plant>();

             thePlant.SetPlant(atlasSpriteName, plant, "description", "variety", germinationTime, growthTime, growthRate);

             thePlant.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f);

             Gesture.onMultiTapE -= WhereToUse;

             this.quantity -= 1;

             button.UpdateButton(this.quantity);

         }
     }

And in the parent class is:

public int quantity;

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
avatar image
1

Answer by the_genius · Mar 29, 2014 at 10:14 AM

use this to find the script with the quantity variable:

var thescript = gameObject.GetComponent("SCRIPT NAME");

use this to access the variable:

thescript.quantity

(this works in unityscript, not sure about C#)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How can I access a GameObjects components using an instantiation variable? 4 Answers

How do I assign script as a variable in the inspector? 1 Answer

Invoking a function in multiple scripts... 2 Answers

iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer

can i change a variable from other js? 1 Answer


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