• 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 cregox · Mar 01, 2010 at 08:10 PM · instantiatevariablescriptingbasics

Instantiate and initialize script

This is actually a continuation from another question I've made before and I'm sorry I couldn't find this in the documentation. I believe this must be there somewhere. What I did found was some other answers on similar subject (coincidentally, a lot of them from SisterKy), but I couldn't abstract them to work in this instance.

I have a script, called GroundCheck.js (can be seen on the other question).

Then I have other scripts calling it.

To each script in which I want to use it, I always use the exact same lines in the function Start to initialize its value, after declaring / instantiating it on global. Simplifying it here:

var groundCheck : GroundCheck = null;

function Start () { if (groundCheck == null) // This is in case the groundCheck isn't choosen within Inspector, as it should groundCheck = GetComponentInChildren(GroundCheck); if (groundCheck == null) // This is in case there is no Component in Children, just to avoid warning messages for debugging purposes groundCheck = gameObject.AddComponent(GroundCheck); }

Note this will have different results for running in different objects, and that's just the way this initialization should work. It's just a safe-back and automated way to associate an object to the variable.

I'm hoping to be able to write this initialization just in one place (probably within GroundCheck.js I suppose) rather than writing it repeated in every script where it's being instantiated like I'm doing now.

Hopefully using some kind of "OnConstruction" over GroundCheck.js or something to be able to declare it along the lines of:

var groundCheck = new GroundCheck();

Can something like this be done?

Comment

People who like this

0 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 StephanK · Mar 01, 2010 at 08:55 PM 0
Share

Why does every object in the hierarchy need a GroundCheck component with the same default values as its children? Where do you set the default values for GroundCheck?

avatar image cregox · Mar 01, 2010 at 09:18 PM 0
Share

The GroundCheck script/class actually have just a "isGrounded" boolean value that's set on runtime for each object. But the groundCheck instance is set to each specific component for each specific object. Some children objects (or maybe even one or none) inside the verifying script should have a GroundCheck attached. I'm just trying to automate the script and make it self contained.

avatar image StephanK · Mar 01, 2010 at 10:41 PM 0
Share

Do you use this component together with another component? then you could either add that value to that component or let the depending component require the groundcheck component.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by runevision · Mar 03, 2010 at 09:24 AM

Okay, so we're talking about the 4 lines of code you currently have in your Start method of (I assume) multiple different scripts, and the problem is that you have to repeat those 4 lines of code in many different scripts, like in say ScriptA and ScriptB. Is that correctly understood?

You can't have the initialization of ScriptA and ScriptB being done by GroundCheck unless GroundCheck knows about ScriptA and ScriptB. In order for GroundCheck to be able to handle ScriptA and ScriptB using the same code, ScriptA and ScriptB need to either inherit from a common class (like GroundCheckUser) or implement an interface (like IGroundCheckUser).

You could then make GroundCheck search for components that is of the type of that class or interface, and perform the necessary initialization on those components.

If you go for making a common parent class, you could also have the initialization inside that class.

Another method which does not require interfaces or parent classes is to make a function that performs the initialization, so you can cut your 4 lines down to one line (the function call). This function could for example be a static function in GroundCheck.

Comment
cregox

People who like this

1 Show 3 · 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 cregox · Mar 04, 2010 at 01:54 AM 0
Share

I think you got the question right now. I thought the script was already a class in itself, from the documentation. How would you employ that "common parent class" idea? I've tried static function, but it doesn't allow me to use non-static methods such as GetComponentInChildren. But that did gave me an idea...

avatar image runevision ♦♦ · Mar 04, 2010 at 09:25 AM 0
Share

Yes, ScriptA and ScriptB are classes by themselves, but they're TWO DIFFERENT classes, and you want the initialization in a single spot as far as I understand. As for the static method, you can use GetComponentsInChildren if you pass a variable to the static function with the object that GetComponentsInChildren should be called on.

avatar image cregox · Mar 09, 2010 at 09:36 PM 0
Share

@Rune, try using @ next time so I can get notified. StackExchange freaking limitations... Unless the comment is under my question / reply of course. Anyway, the "great" idea I had was using static and passing the parameters to the function. It looks awful and bloated, but it works and it does reduce a lot of lines. Next step would be reducing the parameters, but I have no idea how and don't care enough to go that far for now. This will be marked as answered thanks to the Watson effect. ;)

avatar image

Answer by runevision · Mar 02, 2010 at 10:42 AM

Like spree mentioned, if you use have another script component that uses this GroundCheck component, you could let the depending component require the GroundCheck component using RequireComponent. This way, whenever you add a script component that uses the isGrounded variable in GroundCheck, the GroundCheck script component will automatically be added.

Comment
cregox

People who like this

1 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 cregox · Mar 02, 2010 at 11:04 PM 0
Share

I was actually already using RequireComponent. Maybe I did not make myself very clear on the question. I'll try to edit it.

avatar image

Answer by cregox · Mar 09, 2010 at 09:46 PM

Thanks to Rune's answer I got to this solution, for GroundCheck.js. This is it at its full:

#pragma strict

@System.NonSerialized var isGrounded : boolean = false;

function OnTriggerStay () {isGrounded = true;} function OnTriggerExit () {isGrounded = false;}

function OnCollisionStay () {isGrounded = true;} function OnCollisionExit () {isGrounded = false;}

static function start (groundCheck : GroundCheck , getComponentInChildren : System.Object , transformFind : Transform , gameObject : GameObject ) : GroundCheck { if (groundCheck == null) // This is in case the groundCheck isn't choosen within Inspector, as it should groundCheck = getComponentInChildren;//GetComponentInChildren(GroundCheck); if (groundCheck == null) { if (transformFind != null) groundCheck = transformFind.GetComponent(GroundCheck); } if (groundCheck == null) // This is in case there is no Component in Children, just to avoid warning messages for debugging purposes groundCheck = gameObject.AddComponent(GroundCheck); return groundCheck; }

With that I don't get the line the way I wanted, but at least I get to do the initialization on Script A, B, C, etc, in just one (big bloated) line and the instantiation:

var groundCheck : GroundCheck;

function Start () { groundCheck = GroundCheck.start(groundCheck, GetComponentInChildren(GroundCheck), transform.Find("GroundCheckCollider"), gameObject); }

Comment

People who like this

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

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

No one has followed this question yet.

Related Questions

Referencing variables / functions on another script 1 Answer

How to set/get/change variables for newly spawned gameObjects? 1 Answer

Creating & accessing a function from a diffrent "OnTriggerEnter" Function 1 Answer

Putting instantiate inside a variable 2 Answers

How to create a Graph in Unity 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