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 vvkkumar06 · Dec 19, 2013 at 03:37 PM · prefabmultiplayergetcomponent

How to pass a variable value from a script attached to a scene object to prefab?

Actually I was creating a Billiard game, I was calculating power from "gui texture" and then sending it to a script which was attached to cueball but both were in scene. Now I am creating it multiplayer so I have made cueball a prefab and removed it from scene. How can I send "power" value attached to an object in scene to a script attached to prefab which is in project?

I was using code earlier as shown below:

other = GameObject.Find("CueBall");
other.GetComponent("script").power = thePower*50;
Now "thePower" is a variable of an object which is in scene but the "power" is a variable in project as it is attached to prefab(CueBall).

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
Best Answer

Answer by robertbu · Dec 19, 2013 at 03:50 PM

You cannot send anything to a prefab since a prefab does not exist in the scene. But if you are talking about a game object in the scene created from a prefab:

Javascript:

 other = GameObject.Find("CueBall");
 other.GetComponent(script).power = thePower*50; 

C#:

 other = GameObject.Find("CueBall");
 other.GetComponent<script>().power = thePower*50; 
Comment
Add comment · Show 6 · 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 vvkkumar06 · Dec 19, 2013 at 04:26 PM 0
Share

It does not works with an instance of object. I am doing it.

avatar image robertbu · Dec 19, 2013 at 05:11 PM 0
Share

What is 'not work' mean? Not compile? Generate a null reference exception? Are you sure that the name in the Find() is identical to the name on the game object in the scene? Are you sure that the 'CueBall' object exists at the time this code is executed?

avatar image vvkkumar06 · Dec 19, 2013 at 06:11 PM 0
Share

it works when I don't use prefab. But when I use prefab then it show null reference.

avatar image vvkkumar06 · Dec 19, 2013 at 06:15 PM 0
Share

Should I instantiate in Start() actually I am crating an instance of prefab in Update.

avatar image vvkkumar06 · Dec 19, 2013 at 06:18 PM 0
Share

But it won't work in start() because I am using Network.Instantiate

Show more comments

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

18 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

Related Questions

Unable to assign prefabs target at runtime 1 Answer

In Unity can you make games that have online features and how can you test it to see if it works 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Running a script function using "GetComponent" 1 Answer

Using multiple instances of the same script 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