• 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
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

People who like this

0 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, Maybe you want to just declare it outseide the function and initialize it inside startup()..

2 Replies

  • Sort: 
avatar image
Best Answer

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

I figured it out! I had to use BroadcastMessage.

Comment

People who like this

0 Show 0 · 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

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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

SmoothFollow script facing forward 1 Answer

I want to put main camera floating 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