• 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 GadgetGirls · Aug 01, 2014 at 02:42 AM · javascriptinstantiateprefab

Accessing a script on a prefab after Instantiate

I know this has been asked before, but I think maybe either the answers I've found have not been for JavaScript or are for older versions of Unity because I keep getting errors that I'm not "denoting a proper type".

Basically I want to Instantiate a prefab, then call a function that is part of the script on that prefab to set some initial variables. my function is called Init

 var newPrefab:GameObject = Instantiate(prefab, spawnLocation, Quaternion.identity) as GameObject;
 
 var initOnNewPrefab : InitScript = newPrefab.transform.GetComponent(Init);
 initOnNewPrefab.Init(i,tempColor1, tempColor2);

this line

var initOnNewPrefab : InitScript = newPrefab.transform.GetComponent(Init);

was adapted from another answer I found. I found a similar one that would have written it like this

var cloneScript : NameOfScript ;

cloneScript = newPrefab.gameObject.GetComponent(NameOfScript) as NameOfScript ; cloneScript.FunctionNameInItImTryingToDo() ; But I get the same error here. My guess is that I need to import some sort of library to access a variable that stands for a script name. At least that's how I read these two examples. But I don't what the library would be. The other thing I have tried is var newPrefab:GameObject = Instantiate(prefab, spawnLocation, Quaternion.identity) as GameObject;
newPrefab.GetComponents(Init(i,tempColor1, tempColor2));
But this gives me "Unknown identifier:init" Again I feel like what I'm missing is importing in my script so it's usable.
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 commodore · Aug 01, 2014 at 03:50 AM 1
Share

I would instantiate the prefab from InitScript then have a script attached to the prefab that calls the Init function directly

 // InitScript
 // Instantiate your prefab from here

 var tempColor1 ...
 var tempColor2 ...
 
 var newPrefab : GameObject = Instantiate(prefab, spawnLocation, Quaternion.identity) as GameObject;






 // PrefabScript
 // reference InitScript for tempColor vars and call Init function directly 

 private var initScript : InitScript;

 function Awake()
 {
     // Set up the reference to InitScript
     initScript = FindObjectOfType(typeof(InitScript));
 }

 function Start()
 {
     Init(i, initScript.tempColor1, initScript.tempColor2);
 }

 function Init(...)
 {
     ...
 }
 













avatar image GadgetGirls · Aug 01, 2014 at 01:07 PM 0
Share

Okay thanks. I see what I've been doing wrong. I've been looking at Unity sort of inside out, I think because of how the hierarchy window looks. I've been treating sprites as models ins$$anonymous$$d of as views, and the empty game objects at the top as views ins$$anonymous$$d of models/controllers. Now I see why I was so confused and things weren't working the way I was expecting.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Aug 01, 2014 at 04:10 AM

You have

 var initOnNewPrefab : InitScript = newPrefab.transform.GetComponent(Init);

But those are two different types. The type of the component must match the type of the variable. If the error says that Init is unknown, then you have no type called Init, simple as that. So you have to use the correct type.

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 GadgetGirls · Aug 01, 2014 at 04:19 AM 0
Share

Your answer makes sense to me, but what I don't get is what sort of variable I need to create to be able to access my function. If the component I'm trying to get is a script, should I be using a String that is equal to the name of the script? The examples I've found make it seem like there is some sort of Script class in Unity, but I can't seem to find how to access it.

avatar image Eric5h5 · Aug 02, 2014 at 04:38 PM 0
Share

The name of the script is the type. You would not use any strings.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to instantiate a Prefab ? 2 Answers

Instantiate prefabs just before it comes into view 2 Answers

How to instantiate multiple prefabs in multiple positions? 0 Answers

Instantiate prefab within parameters?(Javascript) 1 Answer

Instantiate a Prefab thats been passed through 2 scripts? 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