• 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 jimjames · Feb 21, 2015 at 05:02 PM · prefabgetcomponent

GetComponent from a non Instantiated Prefab

I have a different script which sets and changes this scripts "turretSkill" gameobject. Then in this script I would like to set the component. "TuuretControl" is a script attached to each "turretSkill" object that will be set in this gameobject.

 public GameObject turretSkill;
 private TurretControl turretControl;
 
 void Update()
     {
         if(turretSkill)
         {
             print ("Have turretSkill");
 
             turretControl = turretSkill.GetComponent<TurretControl> ();
 
             if(turretControl) //will not assign this variable 
             {
                 print ("Have turretControl"); //this will not print out
             }
 
 
         }
     }

But this is not working. The "turretSkill" GameObject is a prefab and is not instantiated in the scene. Just need this to grab certain variable amounts for a info box that displays turrets damage and etc.

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 Mmmpies · Feb 21, 2015 at 05:24 PM 1
Share

You can't is the short answer. You'll need to store these details in a script attached to a gameObject (even an empty one will do) in the scene.

When you instantiate the turret get you can pick up that turrets details from the script as well.

avatar image jimjames · Feb 21, 2015 at 05:57 PM 0
Share

I was trying to avoid this, but looks like I cant. Thank you for your input.

1 Reply

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

Answer by andrew-lukasik · Feb 21, 2015 at 05:36 PM

I think you are looking for static field or static class. Because static field does not require class instance to be accessed. But I'm not sure what you need exactly ps: beware overusing static fields.

Comment
Add comment · Show 1 · 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 jimjames · Feb 21, 2015 at 05:52 PM 1
Share

I was trying to stay away from creating more static variables, and was hoping that this would be possible. And yes I am aware of over usage. Thanks

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

Accessing prefab components 1 Answer

Get Component from Instantiated Prefab 1 Answer

Problems getting values from a prefab's script using GetComponent 1 Answer

Remove HingeJoint2D from the Instance not from the Prefab 0 Answers

How to determine if a Player collides with a particular Prefab in an array of Prefabs? 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