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 FXBaconGames · Aug 19, 2014 at 02:36 AM · camerajavascriptvariablessmoothfollow

Changing a variable on a different object

Ok, I realize that there are a bunch of other questions that have been answered about this, but none of those seem to work for me. I have player, a car, and a camera. The camera uses the Smooth Follow script to follow the player. The variable used for the target is this:

 var target : Transform;

The car is supposed to be the target of the camera when a function is executed. The function is executing, but it is not able to find the variable. Here is my code for that:

 var camm = Camera.main.GetComponent("SmoothFollow");
 
 var trans : Transform;
 camm.target = trans;

I dragged the car model onto the the trans cariable in the unity editor. When I do these errors:

UnityException: You are not allowed to call this function when declaring a variable.

get_main can only be called from the main thread.

ArgumentException: get_main can only be called from the main thread.

MissingFieldException: UnityEngine.Component.target

Any ideas? Thx in advance!

Comment
Add comment · Show 1
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 TSRajesh · Aug 19, 2014 at 03:22 AM 0
Share

Nothing much visible from teh code you copy-pasted.. But just checking.. Are you calling teh Camera.main.GetComponent() from outside a function? If so, $$anonymous$$aybe you want to just declare it outseide the function and initialize it inside startup()..

2 Replies

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

Answer by FXBaconGames · Aug 19, 2014 at 10:23 PM

I figured it out! I had to use BroadcastMessage.

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 DBar · Aug 19, 2014 at 05:38 AM

1) Use SendMessage from the gameManager or something related to your game logic using OOP or pass the transform to a static variable inside the Smooth component.

2) If you use SendMessage(), the message has to have a reference inside the params to the Player transform or a gameobject acting as a center of camera in your case, inside the car for granularity.

3) The receiver, the camera object has to have an static variable acting as target.

4) You need to declare a method to set the variable.

Hint:

static var target : Transform;

Your objects has to communicate.

Try to refactor your code to use OOP and not just drag & drop.

Enjoy.

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

24 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

Related Questions

2D Camera Smooth Follow with Dynamic Offset 0 Answers

Smooth Follow Problem 1 Answer

Smooth camera follow return 1 Answer

Demons Souls Fall death Camera 1 Answer

custom SmoothFollow script camera position won't change 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