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 pr0te · Mar 14, 2012 at 12:22 PM · vector3variablefromrefer

Get vector3 from another script

Hey guys.

I'm having some problems with getting a vector3 variable from another script.

 public Vector3 krudtPosition;

In Update(), I'm running this next script through once, using a bool checking if it has just been done.

 krudtPosition = new Vector3(30.50F,krudt.transform.position.y,krudt.transform.position.z);

This works fine when I debug it. The problem comes when I want to get it in another script.

First I define this.

 public Grid krudtScript;

Grid is the name of the script where I want to get the vector3 from.

Then, in the Start() i do the following:

 public GameObject krudt;
 GameObject theKrudt = GameObject.Find("Krudt");
 krudtScript = GetComponent("Grid") as Grid;

And lastly, I'm using it like this:

 krudt.transform.position = krudtScript.krudtPosition;

I've also tried this, but to no luck: krudt.transform.position = new Vector3(krudtScript.krudtPosition);

I'm getting this error: NullReferenceException: Object reference not set to an instance of an object

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 Kleptomaniac · Mar 14, 2012 at 12:29 PM 0
Share

Not great with C#, but try this ins$$anonymous$$d:

 public GameObject krudt;
 GameObject the$$anonymous$$rudt = GameObject.Find("$$anonymous$$rudt");
 krudtScript = the$$anonymous$$rudt.GetComponent("Grid") as Grid;

You weren't referencing that the 'krudtScript' was a GetComponent from the other object. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 14, 2012 at 12:31 PM

That's simply because you implicitly use:

this.GetComponent()

instead of

theKrudt.GetComponent()

So you search for this script on the gameobject this script is attached to and not on the other where it actually is.

Comment
Add comment · Show 2 · 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 pr0te · Mar 14, 2012 at 08:44 PM 0
Share

Even with that done, I'm still getting the same error: NullReferenceException: Object reference not set to an instance of an object

at this line: krudt.transform.position = krudtScript.krudtPosition;

avatar image Bunny83 · Mar 14, 2012 at 09:53 PM 1
Share

Ok you posted quite unrelated and very confusing code snippets.

You should make yourself clear about your setup, on which object is which script and then how to access those objects.

You have two variables:

 public GameObject krudt;
 GameObject the$$anonymous$$rudt;

It seems krudt is a member variable of your script, but is it initialized anywhere?

the$$anonymous$$rudt looks like a local variable inside your function.

If the Grid script is attached to the object you want to move, why don't you assign the position inside that script?

A NullReferenceException happens when you try to use / access a reference variable, but it's not initialized. $$anonymous$$eep in $$anonymous$$d all those functions like GameObject.Find() or GetComponent() can return null if the object doesn't exist.

You have to debug your scripts yourself. Use some Debug.Log() and print the value of the variables to know which one is null. When you figured this out you can search for the reason. $$anonymous$$aybe your object isn't named right? $$anonymous$$aybe there are more than one objects with that name?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

Can't access variable from another script. 1 Answer

Speed updated from another script, but doesn't persist 1 Answer

addition of vector3 1 Answer

Remaping values? 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