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 DoubleONick · Dec 19, 2012 at 06:14 PM · prefabclassaccess

How do I script a prefab like I would code a class?

I have actually developed the core pieces of a game in C++ before I discovered Unity in my search for platform independence. In C++, I could create, say a "robot" class, and give it public members like "sensors", which would be a struct. This sensors struct would have variables for the x and y location of the sensor (used for rendering in OpenGL), sensor type and so on. To access or manipulate properties of the sensor, all I would have to do is create a robot (call it, "myrobot") and say myrobot.sensors[i].type and I could assign or compare values to myrobot's "i-th" sensor's type.

As far as I understand, prefabs are more or less like creating classes like the robot class I had. For example, I created a cubed robot composed of smaller cubes that are grouped together as a prefab, or in a prefab, whichever is the correct way of saying that. Furthermore, I named these smaller cubes, some of them have names like Sensor0, for example. I would like to be able, in a java script, reference Sensor0 of one of my robot prefabs, and assign attributes to, or query attributes of it. Is it possible in some way to treat a prefab the way I treated my C++ robot class? If so, how?

Thank you in advance for any help with this.

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 cagezero · Dec 19, 2012 at 09:59 PM

Yes. What you are wanting to do is totally possible. However, your design will need to change slightly to work in Unity. First off, your understanding of a Prefab is close but not quite there. See this post for a little clarification. Basically a Prefab is not a Class but a collection of one or more GameObjects.

As for accessing individual sensors of your robot, I would suggest creating a Sensor MonoBehaviour and attach it as a Component to any GameObjects in your Robot Prefab hierarchy that you would like to behave as a Sensor. There are probably endless ways of storing references to these Sensors in code, but I will give an example of a way I have done it in the past (sorry for the C# but hopefully it will still give you the right idea).

 public class Robot : MonoBehaviour {
     
     Sensor[] sensors;
     
     void Awake() {
         
         //Get all the Sensor Components in the children of the robot prefab root
         sensors = GetComponentsInChildren<Sensor>();
         //Use Linq to order those sensors by name, assuming they are named Sensor0, Sensor1, etc...
         var orderedSensors = from sensor in sensors
                             orderby sensor.name.Length, sensor.name
                             select sensor;
         //Store the ordered sensor array for later use
         sensors = orderedSensors.ToArray();
     }
 }
 


Comment
Add comment · Show 6 · 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 DoubleONick · Dec 19, 2012 at 11:39 PM 0
Share

cagezero, thank you for your help. I think I can read between the lines, but thought I might ask for some clarification on one point at the moment. In your example code, you have a definition for a class called Robot, which has an array of type Sensor as a member. I am assu$$anonymous$$g there should be a Sensor class somewhere else, also defined as a $$anonymous$$onoBehaviour? In which case, the Sensor class would a) control everything relevant to Sensors and b) be attached to the GameObjects I would like to act as sensors. Likewise, the script that would contain this Robot class you started to define, would cover Robot specific functions and be attached to the Robot prefab (or to instances within the scene, I'm not sure which).

I am also not sure what "Awake" is supposed to do, but assu$$anonymous$$g it's a library thing like "Update", I'll see if I can look that one up.

avatar image cagezero · Dec 19, 2012 at 11:53 PM 0
Share

Yes. You will need to have a Sensor class that extends $$anonymous$$onoBehaviour. Your points a and b are also correct. Theoretically the root of the the Robot prefab hierarchy would have the Robot Class attached as a component and your Sensor Components would be attached where ever you want them (probably to child GameObjects of the Robot prefab).

Awake is called when your Robot instance is being loaded in the scene.

avatar image cagezero · Dec 24, 2012 at 07:54 PM 0
Share

If this answer has helped you please consider marking it as correct :)

avatar image DoubleONick · Dec 27, 2012 at 04:32 AM 0
Share

Sorry. I have marked the answer as correct. I had been leaving it only because I am still working out the details of implementation. It is odd to write classes that use other classes without "including" them. Not to mention, I am still working on how to use the name I gave a Game Object in the Hierarchy in a (java) script. This is an issue at two levels: referencing prefab instances such as (for the sake of argument) "Robot1" and "Robot2", and referencing Sensor0 through SensorN, which belong to Robot1 or Robot2.

avatar image cagezero · Dec 27, 2012 at 05:51 AM 0
Share

No worries. I am just glad the answer is useful to you!

I am not sure how to sort game objects with your specified na$$anonymous$$g convention in js. You might consider opening another question regarding that. However, I do use that sort of functionality sometimes, and the C# code I posted works like a charm. Have you considered creating a C# script that performs only the sorting? You could then use this from any of your js scripts to sort your arrays.

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

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

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Arrays In Classes? 2 Answers

Execute script of parent first 1 Answer

Static target in the srcipt for my Prefab 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