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 dot · Oct 06, 2010 at 08:55 PM · nullreferenceexceptionfunctionrubikscube

problem with calling a function from THIS script, on an object that I find with THIS script.

Let's say I have a script "cubeController.js" put on an empty object.

function enparenting(){

 //looking for central piece
 for(var cube:Transform in slice)
     if(cube.CompareTag("centralCube")) pivot = cube;

 for(var child: Transform in pivot) child.parent = null; 
 for(var cube: Transform in slice) cube.parent = pivot;

!!! pivot.GetComponent(cubeController).RotateObject(pivot.rotation,Quaternion.Euler(pivot.rotation.eulerAngles + Vector3.forward * 90),1.0);

}

function RotateObject(startRot : Quaternion, endRot : Quaternion, rotateTime : float) { var i = 0.0; var rate = 1.0/rotateTime; while (i < 1.0) { i += Time.deltaTime * rate; transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0.0, 1.0, i)); yield; } }

I have an array of objects with no scripts on. array is called "slice" (I do) I need to find one that has a tag called "centralCube", (it works) and after the parenting is done (it works too) I need to call function "RotateObject" on this centralCube. which doesn't work.

I guess it's some kind of "call a function on an object to which the script is not attached" problem, but so far I haven't find an explanation for this "Null" error. "not set to an instance of an object". I tried changing the information passed to the rotateObject function, but with no effect. do you have an explanation?

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
0

Answer by skovacs1 · Oct 06, 2010 at 09:32 PM

To call a function:

  1. The function must exist
  2. You must have access to the function

You are calling pivot.GetComponent(cubeController) which returns the cubeController. It would seem that cubeController is a script. You then try calling a method contained in cubeController called RotateObject that takes those parameters. You cannot call a method from a script that doesn't exist in that script, so if the definition for cubeController does not contain a function definition for RotateObject, you cannot call cubeController.RotateObject as you are trying to do. You would need to define this function in the cubeController script to be called as cubeController.RotateObject as you are attempting to do.

You then define a function RotateObject in thisScript. I assume this is the function you are intending to be calling, but you just defined a function thisScript.RotateObject. To call the method in this script, you would have to attach thisScript to your object and get the instance of thisScript on that object when you call Rotate.

As an alternative to calling it as a method, you could pass the function the transform to rotate and operate on that.

//Call this with
//RotateObject(pivot, pivot.rotation, 
//    Quaternion.Euler(pivot.rotation.eulerAngles + Vector3.forward * 90) , 1.0);
function RotateObject(center : Transform, startRot : Quaternion, 
                  endRot : Quaternion, rotateTime : float) {
    var i = 0.0;
    var rate = 1.0/rotateTime;
    while (i < 1.0) {
        i += Time.deltaTime * rate;
        center.rotation = Quaternion.Lerp(startRot, endRot,
            Mathf.SmoothStep(0.0, 1.0, i));
        yield; 
    }
 }
Comment
Add comment · 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 dot · Oct 06, 2010 at 09:54 PM 0
Share

hmm but the example that I showed IS actually a fragment of the "cubeController" script. so it does have a RotateObject function defined inside it. can I call rotateObject - from cubeController script, which isn't added to the object that I want to rotate? can I call it on an object that doesn't havve this, or any other scripts on it, or will I have to make a "rotateObject.js" and add it to the object that I want to rotate, in order to call "rotateObject" on it?

avatar image dot · Oct 06, 2010 at 09:55 PM 0
Share

in order to call "rotateObject" on it? ... from another script.

avatar image skovacs1 · Oct 07, 2010 at 02:30 AM 0
Share

No, you can't call a method on a script that doesn't exist. pivot.GetComponent(cubeController) will get the cubeController attached to pivot. If there is no such script, then there's no function to call. As I've said, you can either a)attach a script with that function defined in it or b)re-write the method and pass in the thing you want to act upon.

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

No one has followed this question yet.

Related Questions

How to call a function from another script. 0 Answers

Instantiate inside class function returns NullReference 1 Answer

Calling a function in another script. 1 Answer

access GameObject only when instantiated 1 Answer

Function is Null. It's not possible. why. 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