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 mononull · Apr 26, 2013 at 08:49 PM · javascripttransformcomponentinstantiationcache

Caching Transform Javascript Help

I'm fairly new to using Javascript, have been using C#. I've had a couple of null reference issues when trying to cache components into a class-scoped variable. Like this for example:

 var tran : Transform;
 
 function Start()
 {
    tran = transform;
 }
 
 function IAtePestoForLunch()
 {
    var go : GameObject = new GameObject();
    go.transform = tran;  // <--- NULL tran
 }


I'm wondering if this is okay to do first of all. I was doing this fine in C#. Otherwise I think the issue is how I create the object and add the script. I have a gameobject I created on the fly with 'new GameObject()'. I then AddComponent a script (like above) to it. Is it possible that the Start() function is not run when the script component is added after Instantiation? Or should the Start function run upon adding it as a component? Should I be adding component before Instantiation somehow and how would I do that without using a prefab? Thank you

Comment
Add comment · Show 4
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 Jessy · Apr 26, 2013 at 09:22 PM 0
Share

"tran" sucks. Use C# and call it "new Transform transform", utilizing base.transform.

avatar image mononull · Apr 26, 2013 at 09:29 PM 0
Share

What kind of answer is that. I have to use javascript. Easier to $$anonymous$$ch the young folk.

avatar image AlucardJay · Apr 26, 2013 at 09:53 PM 0
Share

There is a unique relationship between a gameObject and a transform. One cannot exist without the other.

A gameObject always has a transform, and a transform is always in reference to its own gameObject.

I'm sure the Start is running and tran is populated. This can easily be checked by looking in the inspector when you run this. Look and see what appears in the inspector for tran.

So, when you create a new gameObject it already comes with a transform component.

What are you trying to do here?

 go.transform.position = tran.position;

 Instantiate ( tran, ...... );
avatar image mononull · Apr 27, 2013 at 12:06 AM 0
Share

In another script I'm saying something like this:

 var go : GameObject = new GameObject()
 go.AddComponent(ScriptFromOriginalPostAbove);
 go.GetComponent(ScriptFromOriginalPostAbove).IAtePestoForLunch();

The tran variable is null as if the Start function didn't run. I add the component after I create a new gameobject.

0 Replies

· Add your reply
  • Sort: 

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

14 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

Related Questions

Simple Instantiate gameObject problem 3 Answers

[JS] transform.rotation not working 1 Answer

How can i Limit the Mouse X Axis for Mouse Look 3 Answers

Component's transform 1 Answer

How to make an object move 1 axes unit. 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