• 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 DJGhostViper · Nov 05, 2016 at 06:21 PM · prefabscript.variablesnullinstatiate

How to instantiate an object with scripts?

I have a button that I press to instantiate a clone/prefab of the orginal object then when I click again it places the clone. When I instantiate the object it doesn't have any idea what scripts to use because I would have to drag the scripts I want to use into the public variable boxes at the bottom of the script. It is showing them as NULL and I cannot put the scripts into the prefab at all. Why can't I and how would I be able to communicate this clone with other scripts so it can do things based off other scripts? Normally I just use public variables and drag and drop the gameobject that has the script into the boxes but how could I reference a certain script without doing that so the clone already knows what scripts it will be using. Thanks for helping!!!

 public class BuildingClickGUI : MonoBehaviour {
 
 public BuildingScript B2; //This is NULL when object is cloned
     
 public Buildinginfo B; //This is NULL when object is cloned
     
 
     public void OnMouseDown()
     {
         
     if (B.IsHQ2 == true) {
 Debug.Log ("HQ - 2nd Button");
         }
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
1
Best Answer

Answer by Axtrainz · Nov 06, 2016 at 11:52 PM

To gain access to other script that other objects has you do this.

There's 2 ways:

Way 1: Only works if the script you want is on only 1 object.

 public BuildingScript B2;
 
 private void SomeCasualFunction()
 {
 //Assign to B2 that script 
 B2 = GameObject.FindObjectOfType<BuildingScript>();
 
 // Use any field, function of that script
 B2.name = "Yoohoo";
 }

Way 2: If the Script is in more than 1 Object & you want only from one specific object then:

 public BuildingScript B2;
 
 private void SomeCasualFunction()
 {
 //Assign to B2 that script 
 B2 = GameObject.Find("ObjectName").GetComponent<BuildingScript>();
 
 // Use any field, function of that script
 B2.name = "Yoohoo";
 }
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

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

Related Questions

How Mark Prefab Dirty? 1 Answer

Troubles instatiating a gameobject with rotation 0 Answers

How can I improve my trading script? 1 Answer

Is it safe to save values in a prefab instead of using PlayerPrefs? 0 Answers

How to tell the number of instantiated object 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