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 acuariano91 · Jul 18, 2014 at 04:09 PM · gameobjectprefabdatabasenull

Can't retrieve information from GameObject

Good afternoon.

I'm currently having trouble trying to retrieve information from a gameObject. And maybe there's a workaround what I'm doing while using another more-db-like-approach, which would appreciate very much.

Trouble is, I have the following JS:

 var Realname: String;
 var description: String;
 var price: float;

That JS is assigned to a prefab (gameObject).

Now I have another JS :

 var currentObject : GameObject;
 function Start () {
 Debug.Log(currentObject.name);
 Debug.Log(currentObject.description);
 }

After I assign the prefab to currentObject in the inspector , my debug.log results are:

1) prefab name (not the Realname var)

2) NULL

Any ideas?

Thanks a bunch.

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

2 Replies

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

Answer by YoungDeveloper · Jul 18, 2014 at 04:16 PM

Hi, GameObject itself does not contain those values, GameObject contains scripts and scripts contain it. Use getcomponent to access others scripts. I explained it ver clearly in this answer: http://answers.unity3d.com/questions/730088/transform-var-between-scripts.html There's an example in c# and unityscript.

Also dont forget to drag and drop the go on the public variable.

Comment
Add comment · Show 4 · 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 YoungDeveloper · Jul 18, 2014 at 04:27 PM 0
Share
 var myScript:ScriptName = currentObject.GetComponent(ScriptName);
 Debug.Log(myScript.description);
avatar image acuariano91 · Jul 18, 2014 at 06:07 PM 0
Share

I've marked this as correct as though I've NEVER been able to load "Scripts" in JS that way (calling it with : SCRIPTNA$$anonymous$$E). I never made the attempt with C#.

$$anonymous$$y final approach was to do this :

The JS assigned to the prefab with the following data :

 var Realname: String;
 var description: String;
 var price: float;

is called ObjectDB.

Then, to retrieve the data I used:

 var propertiesCurrentObject = currentObject.GetComponent(ObjetoDB);
 
 Debug.Log (propertiesCurrentObject.getRealName());
 Debug.Log (propertiesCurrentObject.getDescription());



So in a way I was able to make use of both your answers guys, thanks a bunch!

Regards, hope it helps someone else!

Good day.

avatar image YoungDeveloper · Jul 18, 2014 at 07:08 PM 0
Share

ScriptName, should name of your script. And that's exactly what you did in your second attempt.

avatar image acuariano91 · Jul 21, 2014 at 01:10 AM 0
Share

Oh yes, but the problem is on the compiler if I declare punctually as :

 var propertiesCurrentObject : ObjectoDB = currentObject.GetComponent(ObjetoDB);

I don't know why, but at least JS doesn't like that call.

avatar image
0

Answer by Fearthefrozt · Jul 18, 2014 at 04:15 PM

You would have to access the Realname variable in your other JS script.

Debug.Log(currentObject.Realname);

If this initially gives you an error it's probably because the Realname variable is not accessible from the other script, you could change this by saying:

public var Realname: String;

when you create it, or a better way is to create a getter function in your first script, outside of any other functions that returns Realname:

public function GetRealName(){ return Realname; }

and then call that from the second script where you want to get it:

Debug.Log(currentObject.GetRealName());

The second way is the better way when it comes to security and proper coding techniques.

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 Fearthefrozt · Jul 18, 2014 at 04:17 PM 0
Share

This may not be the exactly correct code because I usually code in C# but the concept should be correct.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Clone a prefab 1 Answer

Objects flickering between dark and light 7 Answers

Get the name of an instance's prefab at runtime? 0 Answers

Create with code 2.2 pizza not shooting 1 Answer

difference between prefab and gameobject in listening event 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