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 ChrisSch · Sep 26, 2013 at 09:54 PM · guitextureassigngameobject.find

Assign GUITexture in scene to variable via scripting?

Ok guys what am I doing wrong here? And what am I suppose to do?

Since I have over 60 levels so far where I need to drag and drop 3 in-scene gameobjects, two being textures and one being a gameobject, its gonna be a real bother doing it in each level with drag and drop. So how do I assign them with a script at start?

Here's one of them, and the way I tried but nothing happened:

 var fuelMeter : GUITexture;
 
 function Start()
 {
     fuelMeter = GameObject.Find("FuelMeter/Fuel").GetComponent(GUITexture);
 }

So the question is, how do I, by name, assign the GUITexture called Fuel, which is a child of FuelMeter, to the variable fuelMeter which is a GUITexture?

Comment
Add comment · Show 3
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 ChrisSch · Sep 26, 2013 at 11:26 PM 0
Share

Anyone? :S

avatar image Cheetoturkey · Sep 27, 2013 at 01:10 AM 0
Share

Is the GUITexture A Prefab?

avatar image ChrisSch · Sep 27, 2013 at 01:22 AM 0
Share

No, like I said, all the objects I wanna assign are in that scene and since I have only one of each its fine if I do it by name, I just don't know how.

1 Reply

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

Answer by robertbu · Sep 27, 2013 at 12:24 AM

How about:

 var fuelMeter : GUITexture;
  
 function Start() {
     var go = GameObject.Find("FuelMeter");  // Find the parent game object
     var fuel = go.transform.Find("Fuel");   // Get the transform of the child
     fuelMeter = fuel.guiTexture;            // Get the guiTexture from child
 }
Comment
Add comment · Show 5 · 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 ChrisSch · Sep 27, 2013 at 12:40 AM 0
Share

Thanks for the answer but nope, nothing happens, it doesn't assign it.

avatar image robertbu · Sep 27, 2013 at 03:58 AM 0
Share

Note I tested the above code before I posted it. I had a game object called 'Fuel$$anonymous$$eter' with a child object called 'Fuel' that had a GUITexture component. It worked fine. Put some debug.log statements in your code to figure out what is going on. Does it find 'Fuel$$anonymous$$eter'? Does it find 'Fuel'? Is guiTexture found on fuel? Do you have more than one game object that has the name 'Fuel'? Are the strings you are using an exact match to the name of the game objects?

avatar image ChrisSch · Sep 27, 2013 at 10:33 AM 0
Share

Yes your script should work but nothing happens. I have an empty game object called Fuel$$anonymous$$eter, and two childs one Fuel(GUITexture), and the other unimportant and just there for visual purposes, called Fuel $$anonymous$$eter(GUITexture), with a space.

avatar image robertbu · Sep 27, 2013 at 06:37 PM 1
Share

As mentioned in my previous comment, what have you done to track the problem down? Put Debug.Log() statements and verify each part of the code. $$anonymous$$ake sure it is finding the correct 'Fule$$anonymous$$eter'. Start with an empty scene with just your Fuel$$anonymous$$eter and this script does it work for you? If the code above is not working, then there is something else going on that needs to be tracked down.

avatar image ChrisSch · Sep 27, 2013 at 09:14 PM 0
Share

Thank you! Your code was perfectly fine it just had to go before a couple lines related to the fuel meter that prevented it from happening. And as for assigning a game object in the scene to a GameObject variable I can use the same way I assume. Thanks again this will make everything a whole lot easier! :D

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to emit particle from a collision? 2 Answers

getting a sum from two scripts 1 Answer

Rotating a Sphere with the mouse? 1 Answer

Ignore/Destroy then re-enable Mesh Colliders 3 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