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 Cranberry · Feb 09, 2014 at 11:18 AM · crashcomponentaccess

Unity crashes when accessing a function of a script from another script

Hey guys! I'm trying to access a function of a script from another script. My first script EnterVehicle.cs calls the SetTarget function of FollowCamera.cs like this:

  mainCamera.GetComponent<FollowCamera>().SetTarget(enteredCar);


SetTarget is declared as follows:

   public void SetTarget(Transform _target)
   {
         target = _target;
   }


Unity keeps crashing when I call SetTarget from my EnterVehicle script.

Unity has been really unstable since I updated: I don't get any runtime errors unity just freezes and I have to close it from the task manager.

Has anybody got an idea what I'm doing wrong? Thanks in advance, Cranberry

Comment
Add comment · Show 3
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 whydoidoit · Feb 09, 2014 at 11:20 AM 0
Share

It's probable that you have created an infinite loop.

Attach $$anonymous$$onoDevelop to the process and pause it when it freezes to see where in your code the problem is happening.

avatar image Cranberry · Feb 09, 2014 at 11:27 AM 0
Share

Ok so I found out that it doesn't even get to 'target = _target;' unity just freezes when it gets to mainCamera.GetComponent.

avatar image DajBuzi · Feb 09, 2014 at 01:55 PM 0
Share

I do not see any of my answers so i'll just use comments. Your method/class is not static so i think that you cant access it directly like you did. Try to first make reference to the FollowCamera script:

 FollowCamer followCamera = mainCamera.GetComponent<FollowCamera>();

Then try to access your method like this:

 followCamera.SetTarget(enteredCar);

2 Replies

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

Answer by Cranberry · Feb 09, 2014 at 03:03 PM

Stupid me! facepalm Forgot to assign the Transform for the vehicle in the editor. But I still got the problem that Unity just freezes instead of giving me a NullReferenceException.

Comment
Add comment · 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
0

Answer by DajBuzi · Feb 09, 2014 at 03:07 PM

I might be wrong with this but what i see is that your method is not static so you need a reference to the script object.

 var camScript = mainCamera.GetComponent<FollowCamera>();

And then you can perform actions with your FollowCamera script.

  camScript.SetTarget(enteredCar);
Comment
Add comment · 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

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

20 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

Related Questions

Declare Component type in GetComponent 2 Answers

Very simple component-access question 0 Answers

Can I access components scripts? 3 Answers

Painted Tree's Script Component 1 Answer

Access to variables from other script objects 3 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