Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CybernetHacker14 · Sep 14, 2018 at 06:15 PM · scripting problemscripting beginnerscriptingbasics

How to return control back to the original script?

In a script, I have to access a public function in another script to execute it's functionality. I usually do this via someGameObject.GetComponent<SomeComponent>().publicFunction(). But the problem that occurs is the control won't come back to the original script after the function execution is over. Suppose the above statement is between some lines of code like -

 statement 1
 statement 2
 statement 3
 someGameObject.GetComponent<SomeComponent>().publicFunction()
 statement 4
 statement 5
 statement 6

The statements from 4 to the end of the current function from which it's being called are getting skipped, or ignored because the execution control never returns back from the other script to execute the remaining statements. How can I solve this? Thanks in advance.

Comment
Add comment · Show 12
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 dan_wipf · Sep 14, 2018 at 06:26 PM 0
Share

hm maybe outsource it in a coroutine?

avatar image CybernetHacker14 dan_wipf · Sep 14, 2018 at 06:52 PM 0
Share

@dan_wipf I'll give it a try. Thanks.

avatar image TreyH · Sep 14, 2018 at 07:13 PM 0
Share

What do you mean by "return control"? What is "control" here and what is keeping you from calling it anywhere?

avatar image CybernetHacker14 TreyH · Sep 14, 2018 at 07:40 PM 0
Share

By control I meant control flow. And by return control I meant that the flow of instruction execution should return back to the original script from where I called the function to the another script. In my mentioned example, the flow should go from statement 1 to 3, then execute the function in another script, then execute statements 4 to 6.

avatar image TreyH CybernetHacker14 · Sep 14, 2018 at 07:46 PM 0
Share

That should be fine then. Are there any issues in the console? There's no reason for the block being executed (that makes the call to your other component's function) to do anything but continue, unless an issue was encountered in the function.

Are you sure that the component isn't null?

Show more comments
avatar image BastianUrbach · Sep 14, 2018 at 07:39 PM 0
Share

Sounds strange. A function only ends if: 1) It reaches the end 2) It reaches a return statement 3) An exception is thrown (i.e. an error occurs) One of those three things must be happening. Do you get an error message? Can you post some of the code?

avatar image CybernetHacker14 BastianUrbach · Sep 14, 2018 at 07:45 PM 0
Share

The script calling the function :-

 public void SwitchWallpoint()
     {
         _endPoint.SetActive(true);
         _currentControlledWallObject = _endPoint;
         InitialWallPieceSpawn();
         _currentControlledWallObject.GetComponent<BuildingSystems>().ActivateGhost();        
     }
 
     void InitialWallPieceSpawn()
     {
         GameObject wallPiece = Instantiate(_wallPiecePrefab) as GameObject;
         wallPiece.SetActive(true);
         wallPiece.transform.SetParent(_endPoint.transform.GetChild(2).gameObject.transform);
         wallPiece.transform.position = _endPoint.transform.GetChild(2).gameObject.transform.position + new Vector3(0,0,17);
         wallPiece.GetComponent<WallPieceDoublyLinkedList>().wallIndex = 0;
     }

The function that is being called in another script:-

     public void ActivateGhost()
     {
         _ghostRedRender.SetActive(true);
         _ghostGreenRender.SetActive(false);
                 
         foreach (GameObject gObject in _building$$anonymous$$esh)
         {
             gObject.SetActive(false);
         }
         
         _foundation$$anonymous$$esh.SetActive(false);
         _unitPositionPlaceHoldersReference.SetActive(false);
         gameObject.GetComponent<NavmeshCut>().enabled = false;
         gameObject.transform.Find("SelectionBoxHalo").gameObject.SetActive(true);
 
     }
 
avatar image dan_wipf CybernetHacker14 · Sep 14, 2018 at 07:51 PM 0
Share

just wondering does it execute all those bools? have you tried to make a debug.log somtehing at the end of the excutional part? as well after the line where its called in your first script?

Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Sep 14, 2018 at 10:13 PM

Good day.

"the execution control never returns back from the other script"

how? this is not posible, if there is not any "return;", so when the publicFunction() ends, (is not infinite) next will be statement 4.

I can only imagine..:.


1- You are getting an error in that publicFunction and you ahve deactivated the errors alerts


2- The public function is disabling the script or the object containing the script.


3- You have some infinite loop (for example a bod designed "while") in tat publicFunction


I can not imagine other possibilities...

Comment
Add comment · 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 CybernetHacker14 · Sep 15, 2018 at 09:00 AM 0
Share

Good day to you. I double checked again, including all the errors. Nothing turned up as per your mentioned points. 1. No error related to that function is turning up in the game. 2. The script isn't getting disabled. 3. No while loop in the public function.

This is the public function code -

 public void ActivateGhost()
      {
          _ghostRedRender.SetActive(true);
          _ghostGreenRender.SetActive(false);
                  
          foreach (GameObject gObject in _building$$anonymous$$esh)
          {
              gObject.SetActive(false);
          }
          
          _foundation$$anonymous$$esh.SetActive(false);
          _unitPositionPlaceHoldersReference.SetActive(false);
          gameObject.GetComponent<NavmeshCut>().enabled = false;
          gameObject.transform.Find("SelectionBoxHalo").gameObject.SetActive(true);
  
      }

Now I got a temporary fix. I optimized the function a bit, by removing the "Find" operation with a public gameObject reference method. So this

 gameObject.transform.Find("SelectionBoxHalo").gameObject.SetActive(true)

now becomes this

 _selectionBoxHalo.SetActive(true)

where _selectionBoxHalo is a public gameObject which is manually assigned in the inspector by me. So there's no need for the gameObject to be searched when it is now made as a reference. Interestingly, when I code the statements as such

  public void SwitchWallpoint()
      {
          _endPoint.SetActive(true);
          _currentControlledWallObject = _endPoint;
          // Statements under consideration
 
          InitialWallPieceSpawn();
          _currentControlledWallObject.GetComponent<BuildingSystems>().ActivateGhost();
 
          // -------------------//
      }
  
      void InitialWallPieceSpawn()
      {
          GameObject wallPiece = Instantiate(_wallPiecePrefab) as GameObject;
          wallPiece.SetActive(true);
          wallPiece.transform.SetParent(_endPoint.transform.GetChild(2).gameObject.transform);
          wallPiece.transform.position = _endPoint.transform.GetChild(2).gameObject.transform.position + new Vector3(0,0,17);
          wallPiece.GetComponent<WallPieceDoublyLinkedList>().wallIndex = 0;
      }

the ActivateGhost() function doen't get executed, even though the wallPiece.GetComponent<WallPieceDoublyLinkedList>().wallIndex = 0 time-complexity is less. When both the statements are written like this,

 _currentControlledWallObject.GetComponent<BuildingSystems>().ActivateGhost();
     InitialWallPieceSpawn();

the statements work fine. This can be a bit of a problem because even with this much of optimizing, the Unity compiler straight-away gives up execution of statements when cross-scripting reference is used. And the project is in early development stages, so a lot of functionality may be added to it in the succeeding stages, which may again give rise to issues.

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

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

Related Questions

Having trouble using SendMessage 1 Answer

how to allow the key to only open 1 door rather than all of them? 0 Answers

Having trouble coding an on/off toggle for an animation in script. Please help! 1 Answer

store many GameObjects 2 Answers

My player keeps dashing 0 Answers


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