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 ijzerman86 · Aug 29, 2013 at 09:53 AM · function

Cant call function in js another script

Hello there, first post after lurking for a while. I am experimenting with the zigfu toolbox and a kinect. I created a collision with the hand object and a trigger for basic interaction. I want to change a mapping on collision on a third object. I use two scripts that interact with each other. I used the following resource: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

The collision works fine but when i want to trigger the function in the other script it states "object not set to an instance of an object". I do however set the instance in the top of the script and it doesnt give a error on compiling that, only on runtime at the actual collision. The trigger-script is as follows:

 var other : change_mapping;
 other = gameObject.GetComponent("change_mapping");
 
 function OnTriggerEnter (myTrigger : Collider) {
     if(myTrigger.gameObject.name == "TriggerObject"){
         Debug.Log("Box went through!");
         other.ChangeTex01();
     }
 }

I assigned the Other var to the gameobject containing the change_material script. The change_mapping script is as follows:

 public var tex01_texture    :    MovieTexture;
 public var tex02_texture    :    MovieTexture;
 public var temp_tex        :    MovieTexture;
 public var current_tex    :    MovieTexture;
 
 function Awake() {
     current_tex = temp_tex;    
 }
 
 function Update () {
     renderer.material.mainTexture = current_tex; 
 }
 
 function ChangeTex01(){
 
     Debug.Log("change tex01");
 }
 
 function ChangeTex02(){
 
     Debug.Log("change tex02");
 }
 

I feel kinda dumb because there are many posts like this but i just cant get it to work after a day of fidling and googling. Does anybody have a idea? Thanks in advance :)

Comment
Add comment · Show 5
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 meat5000 ♦ · Aug 29, 2013 at 09:58 AM 0
Share

is change_mapping the name of a script on the same object as the script which is trying to call it?

avatar image meat5000 ♦ · Aug 29, 2013 at 10:01 AM 0
Share

Im using this

 var BlockPopperScript : BlockPopper;
 
 function Update()
 {
   //Some Stuff
   GetComponent(BlockPopper).breakBlock(blockTouched); //calls a function breakblock with args blockTouched within the script BlockPopper
 }
avatar image ijzerman86 · Aug 29, 2013 at 10:27 AM 0
Share

change_mapping is the name of the script, its located on a box named cube. trigger script is located on object named RightHand @meat5000, trying your code now :)

avatar image ijzerman86 · Aug 29, 2013 at 11:07 AM 0
Share

I came up with this but it doesnt work.

 var BlockPopperScript : change_mapping ;
 var hit : boolean = false;
 
 function OnTriggerEnter (myTrigger : Collider) {
     if(myTrigger.gameObject.name == "TriggerObject"){
         Debug.Log("Box went through!");
         hit = true;
     }
 }
 
 function Update(){
  if (hit == true){
     //GetComponent(BlockPopper).breakBlock(blockTouched); //calls a function breakblock with args blockTouched within the script BlockPopper
     GetComponent(change_mapping).ChangeTex01(); //calls a function breakblock with args blockTouched within the script BlockPopper
     hit = false;
     }
 }

Its still the same, it compiles, i can run but on collision it states a error. Could it be that some kinect framework gets in the way?

avatar image meat5000 ♦ · Aug 30, 2013 at 09:46 AM 0
Share

If you look closely at my code you will notice that BlockPopperScript is never actually used. You can GetComponent directly from a script that is attached to the same gameobject as the script you are accessing from. Otherwise you need to Find the gameObject the script is on you want to access.

Also, the snippet was something from my game as an example, you shuld probably use your own na$$anonymous$$g :P

1 Reply

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

Answer by Hoeloe · Aug 29, 2013 at 12:16 PM

The issue is that GetComponent is not finding anything. GetComponent searches only for components attached to the current object, so if your script is on a different object (which I believe you said it was), it won't find anything. You have to specify the object the script is attached to, using something like this:

 var obj : GameObject = ...//Get a reference to the object holding the script here
 obj.GetComponent(change_mapping).ChangeTex01();
Comment
Add comment · Show 2 · 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 ijzerman86 · Aug 29, 2013 at 01:10 PM 0
Share

Very nice, thank you! Works a treat :)

avatar image Hoeloe · Aug 29, 2013 at 09:43 PM 0
Share

If my answer solved your problem, you can mark your question as "solved" by clicking the "tick" next to the answer that solved it.

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

18 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 avatar image avatar image

Related Questions

private Vector3 function? 2 Answers

calling c# function from a js file.. 1 Answer

Accessing functions on external JS in the same object 0 Answers

what is the problem of this code ? 2 Answers

[SOLVED] Cycling all the way through weapons properly 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