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 /
  • Help Room /
avatar image
0
Question by mithrandirthegrey01 · May 19, 2016 at 04:59 PM · unity 5prefabinstantiate prefab

problem with prefab's script

I know for sure that this question has been answered before but because in each answer the solution presented was adjusted to someone's classes, variables, etc it was difficult for me , who just begun to work in unity, to understand it.

My problem through a very general example : I create a cube in the Hierarchy using the create button. Then I drag the cube from the hierarchy to the project tab (so im creating its prefab i guess). Then i add the following js script, named Cube_script, to both "hierarchy cube" and "prefab cube":

 #pragma strict
 
 public var variable = 10000000;
 
 function Start () {    
 }
 function Update () {
 
 variable = variable - 1;
 }

Then i hit play and i observe from the inspector that that the variable from the "hierarchy cube" is decreasing but the variable from the "prefab cube" is staying the same. All i want to do is to force the variable of the "prefab cube" to "follow" the variable of the "hierarchy cube".

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 ralphlizard · May 19, 2016 at 05:13 PM

It sounds like you have the "hierarchy cube" in the hierarchy while the "prefab cube" is not. The prefab is not being updated because it is not meant to -- it is simply a reference that you can make instances of in the scene. Your "hierarchy cube" is one such instance. If you want a variable in the prefab to be the same across all instances, you can try making that variable static, like this:

  static var variable = 10000000;
  
  function Start () {    
  }
  function Update () {
  
  variable -= 1;
  }
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 mithrandirthegrey01 · May 19, 2016 at 07:31 PM 0
Share

That didn't work for me. If you want me to elaborate :

I have a second script written in C# and from this script i want to control variable.So i create a GameObject in the C# script and i dragged in the inspector the "prefab cube" because i saw that i can't drag there the "hierarchy cube". And the problem is that the variable of "prefab cube" doesn't change and therefore i can't control it.

So making variable static gave me an error "Static member........cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"

avatar image ralphlizard mithrandirthegrey01 · May 19, 2016 at 08:03 PM 0
Share

What do you mean you "create[d] a GameObject in the C# script"? Do you mean you instantiated a GameObject?

You should not be able to drag any GameObject into the inspector. Do you mean you tried to set a SerializeField (of what script in what GameObject?) in the inspector by dragging something into it?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Prefab with same scripts do the same thing 0 Answers

Instantiating prefab causes existing instances to change values 1 Answer

How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers

Prefabs not instantiating after build 0 Answers

How To Generate Platforms Down Each other in a pattern with some different positions Unity 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