Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
geckoo

Question about fonctions

Question

Again a silly question. In a script JS, how i can call a function who is in an another script?

I read this information in a tutorial but i forgot. Thanks you for support ++

 

 

Share this post


Link to post
Share on other sites

29 answers to this question

Recommended Posts


  • 0

Thx for support :)

I don't have to call something in script to attach this second script at first?

I try it ++

Share this post


Link to post
Share on other sites
  • 0

YD, that was what I was about to post. NEVER use SendMessages like I did in my FPS Kit, it's so much slower and it will slow down your game. Use GetComponent(ScriptName).FunctionName();

 

Use SendMessage only if it's called once, not every frame. BroadcastMessage is even slower so use it ONLY when necessary.

Share this post


Link to post
Share on other sites
  • 0

SendMessage "function-name" has no receiver!

 

I forgot something...

I don't understand boys. Can you explain to me?

I try second method with GetComponent. Thx ++

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

DO NOT USE SENDMESSAGE. USE GETCOMPONENT

 

EDIT:

This might help you...

Please login or register to see this link.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

Show us your script how are you calling sendmessage, and script which is attached to object.

 

@Daboss let him use sendmessage for now, its good for learning ;)

Edited by YoungDeveloper

Share this post


Link to post
Share on other sites
  • 0

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

 

Hum... i don't understand something @-)

I read the tutorial ++

Share this post


Link to post
Share on other sites
  • 0

Post both scripts here. If you are using C#, make sure that the function that you are trying to call is "public", not "private". Make sure the function name is EXACT.

Share this post


Link to post
Share on other sites
  • 0

Seems like everyone is forgetting one. Depending on what the second script is doing, you can also use a static function. I use static functions or static variables when saving information to check against something in another script. But like I said, depends on what your trying to do. Handling states is a great example because it needs to be accessed by so many other components and its just plain faster

Example:

//player state script

static function UpdateMoveState(state : int){playerState = state;}

//player movement script

if(PlayerState.playerState == 0){PlayerState.UpdateMoveState(1);}

Share this post


Link to post
Share on other sites
  • 0

Ok I understood :

var lol : scriptX = GetComponent(scriptX);

lol.functionX();

lol.variableX ++;

etc...

Thx for support. It's a new language for me ++

Share this post


Link to post
Share on other sites
  • 0

That will work only if both script are attached to the same gameobject.

 

...and this is why I did not understand your explanation. Thanks you very much for help ++

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

You can getcomponent like that only if both scripts are attached to the same object. Can you tell what exactly are you trying to do ?

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Yes it is... it's a temporary variable, not a stored variable.

 

Anyway, geckoo, yes you got the use of GetComponent. But you can also get it from a different object by making a new variable at the top of the script and then say...

 

targetObject.GetComponent(ScriptName).DoSomething();

 

 

If you are confused about the variable part, add this to the top

 

public var targetObject : GameObject;

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

На самом деле, я только попробовал понять этот принцип. Я не создал ничего особенного. Я исследовал код и я заметил много аспектов, которые можно изменить/совершенствовать. Чтоб сделать эти изменения, я должен обязательно понять как всё идёт - как один код соединяется с другими... вот почему я задал вам такой вопрос. Спасибо за помощь ++

 

I try to understand how this code was developed and how i can modify many details (it's a begining). It so confused for me and i have so many questions... I was coder - ASM and C/C++ - rootkit, BOF, backdoor, shellcode - an another world, but I forgot many lessons. I work on it. Thanks boys because you helped me.

Share this post


Link to post
Share on other sites
  • 0

всё зависит от ситуации, когда каким способом лучше ползоватса. Как видеш сушествует много способов, но не всегда можно използовать любой способ. А если ешо и думать о производительности, то нужно знать все способы и выбрать правильный.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

Yes, true. I don't think anyone even uses GetComponent for stored variables... but for temporary variables are necessary.

Share this post


Link to post
Share on other sites
  • 0

Трудно мне уже отличать этот метод от других. Даже если я понял теорию, есть ещё практическое упражнение.

Продолжаю искать и вы знаете выражение - кто ищёт, тот найдёт...

Прежде всего, я решил исследовать аккуратно код из fps_tutorial. Этот исходник очень интересен ++

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

I hope you will understand Russian, with Latin typing.

 

Lutse vsego ucitsa, eta realjna stota gjelatj, nje toljka citakj kod, no somamu evo pisatj. Kak govoritsa "practice makes perfect". I tak po cu cutj, i so vremenjim vso bugjet okei. Sovetuju nacatj, s colliders i triggers, eta davoljana ljohkaja tjema. Patom uze getcomponent, sendmessage, raycasting i tok daljie.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...