• 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
Question by dansav · Feb 27, 2012 at 06:40 PM · iphonegetcomponentpragma

pragma strict problem with GetComponent and script name

I'm trying to use pragma strict to prepare a file for the iphone. In the file I have a GameObject and I want to reference a function in another script. I have a function which allows the user to load an object dynamically then attach a script to the object. In this case they load a cow and then attach cowScript.js

This doesn't work. The error is that initialize() is not a member of Component.

 function loadObject(otype:String,oname:String){
 
 //instantiates a cow from resources load first and names it oname
 
 GameObject.Find("cow").AddComponent(otype+"Script");
 GameObject.Find("cow").AddComponent(otype+"Script").initialize();
 }
 
 loadObject("cow","bessie");
 

I'm not sure where the problem is or how to fix it. But I do want to have it so the user can name their object and attach different scripts to it. Is there a workaround that would preserve the dynamic script idea.

Thanks,

Comment

People who like this

0 Show 0
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

Answer by WillTAtl · Feb 27, 2012 at 08:08 PM

pretty sure the string-parameter forms of GetComponent and AddComponent don't work at all in strict mode, so you'd have to refactor to replace those otype+"script" string variables with MonoBehaviors that reference the script, like this...

You also prolly want to keep the gameobject rather than calling find twice, and you definitely don't want to add the component twice!

 enter code here`function loadObject(typeScript:MonoBehavior, name:String)
 {
     //get the object
     var obj:GameObject=GameObject.Find(name);
     //make sure it was there
     if (!obj)
     {
         Debug.Error("Object called \""+name+"\" not found!");
         return;
     }
     //add and initialize the component
     obj.AddComponent(typeScript).initialize();
 }
Comment
dansav

People who like this

1 Show 10 · 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 WillTAtl · Feb 27, 2012 at 08:10 PM 0
Share

so... edit not working? O_o

 function loadObject(typeScript:MonoBehavior, name:String) 
 { 
     //get the object 
     var obj:GameObject=GameObject.Find(name); 
     //make sure it was there 
     if (!obj) 
     { 
         Debug.Error("Object called ""+name+"" not found!"); 
         return; 
     } 
     //add and initialize the component 
     obj.AddComponent(typeScript).initialize(); 
 }
avatar image dansav · Feb 27, 2012 at 08:13 PM 0
Share

That makes a lot more sense than my attempt. I'll try it. Thanks.

avatar image dansav · Feb 27, 2012 at 08:35 PM 0
Share

it doesn't appear that AddComponent wants a monobehaviour...it gives an error with pragma strict on. What does it want? a string?

No appropriate version of 'UnityEngine.GameObject.AddComponent' for the argument list '(UnityEngine.MonoBehaviour)' was found.

I tried spelling MonoBehavior or MonoBehaviour it doesn't like either.

avatar image dansav · Feb 27, 2012 at 08:40 PM 0
Share

that should be MonoBehavior or MonoBehaviour

avatar image WillTAtl · Feb 27, 2012 at 08:47 PM 0
Share

whoops! Yer right, mia culpa. This answer will self-destruct in 5 minutes. If I find a correct answer I'll letcha know.

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Get variable from another object 1 Answer

Help converting javascript to ios iphone scripting 2 Answers

How to access scripts other than by name. 1 Answer

Disabling a script in c# not working for some reason 1 Answer

C# GetComponent Issue 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