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 Ent · Jun 21, 2010 at 07:11 AM · javascriptgetcomponentconversion

changing js public variable from cs script

Hi, Im stuck on changing a variable in a JS script from a CS Script. Can I change the variable directly or do I call a function in the JS script from the CS script?

MouseOrbit.js

var target : Transform;
...code

Original Switch.js

function Update () {
    //if (Input.GetButtonDown("Jump"))GetComponent(Follow).target = switchToTarget;
    if (Input.GetButtonDown("Jump")){
    var newTarget = GameObject.Find("Cube1").transform;
    GetComponent(Follow).target = newTarget;
    }
}

The cs script I am trying:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class SceneControl : MonoBehaviour { public void Start (){ GameObject test = GameObject.Find("Main Camera"); Debug.Log("Got:" + test.name); //MouseOrbit myMS = test.GetComponent<MouseOrbit>(); //Debug.Log("Got:" + myMS.name); Component myMS = GetComponent(typeof(MouseOrbit)); Debug.Log(myMS.ToString()); //myMS.target = sceneObj.transform; }

I keep getting a NullReferenceException : Object reference not set to an instance of an object. This is for the getComponent call. the GameObject.Find works ok and I get Main Camera back. Is this even possible?


K this works:

            GameObject test = GameObject.Find("Main Camera");
        Debug.Log("Got:" + test.name);
        MouseOrbit myMS = test.GetComponent<MouseOrbit>();
        Debug.Log(myMS.ToString());
        myMS.target = sceneObj.transform;

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

2 Replies

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

Answer by Tetrad · Jun 21, 2010 at 07:19 AM

It doesn't matter what language the scripts are written in, a public member variable is a public member variable.

The trick is that you have to be using the right type. So your commented out GetComponent is the correct one.

The non-commented-out GetComponent is getting the component on your SceneControl object and not your Camera object. That's probably why you're geting a null reference exception. Also if you wanted to use the non-generic GetComponent you'll have to cast your return type to the type you want to access. Component doesn't have a "target" variable.

Edit: this may not be entirely accurate. http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

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 Ent · Jun 21, 2010 at 07:17 AM

Ok found something myself

link text

just had to place it into the standard Assets folder

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

create GUI.Label and then access to it.. 2 Answers

GetComponent ? help! 1 Answer

C# conversion of JavaScript, problems with nested class as a custom type 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