Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 JorgenFreeman · Nov 01, 2012 at 03:56 PM · referencec

Trouble referencing variables inside of a script (C#)

Hey, i have two scripts, one called targeting which has the public game object "attackingCity" in it. In the other script, i have a private variable of type targeting called "targetRef". I have set the attackingCity variable to cityE and when I Debug.Log it from the targeting script it confirms this. When i try and call this city from the other script using Debug.Log(targetRef.attackingCity); it throws a null reference exception at me.

Comment
Add comment · Show 2
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 Seth-Bergman · Nov 01, 2012 at 04:25 PM 0
Share

you need to show the code, no telling what you're doing wrong

avatar image JorgenFreeman · Nov 02, 2012 at 08:30 PM 0
Share

private targeting targetRef; targetRef.attackingCity = this;

That's all the code there is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Montraydavis · Nov 01, 2012 at 04:32 PM

You are not properly settings the refernce. Perhaps post your method here, but, it should look something like this

 var TargetReference : GameObject ;
 
 function Start ( )
 {
    TargetReference = GameObject.Find ( "ObjectName" ) ;
     //OR
    TargetReference = GetComponent ( ScriptComponentNameWithout.CS ) ;
 
 }
Comment
Add comment · Show 8 · 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 JorgenFreeman · Nov 01, 2012 at 04:52 PM 0
Share

targetRef = GetComponent("targeting");

That's the line where I'm trying to set the target reference and right now it's saying that i can't convert a type component to a type targeting. When I add 'as targeting' to the end in order to cast it, my Debug.Log(targetRef) returns null. Whats going on? I'm in C# btw

avatar image Montraydavis · Nov 01, 2012 at 04:54 PM 0
Share

C# and JS don't really differe here, except declaring variables.

Just set the Type to the type that it is . . IE

If it's a GameObject

 GameObject TargetReference = blaaa. ;
 ScriptName TargetReference = blaaa. ;
avatar image JorgenFreeman · Nov 01, 2012 at 05:18 PM 0
Share

EDIT: I've resolved part of the issue: when i Debug.Log(targetRef) now it logs "scriptHolder (targeting)" (scriptHolder is the object containing the targeting script.) However, when i try to do targetRef.variableName, it throws up an error that 'UnityEngine.Component doesn't contain a definition for variableName'

avatar image Seth-Bergman · Nov 01, 2012 at 05:21 PM 0
Share

simply put, just get rid of the quotes:

targetRef = GetComponent(targeting); // no quotes

this returns a type of "targeting" rather than "Component", as you want

also, maybe declared wrong, try:

targeting targetRef = gameObject.GetComponent...// ...etc

avatar image JorgenFreeman · Nov 01, 2012 at 05:51 PM 0
Share

It just doesn't work. i had to make the variable of type component because there was a conversion error and typecasting wouldn't work. I need quotes because the GetComponent only takes a string. Check the edit on the previous comment.

Show more comments

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

12 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

Related Questions

Getting parameter values sent to a method c sharp script 0 Answers

Error when trying to parse csv file 1 Answer

Lerping issues (C #) 2 Answers

Trouble setting a variable to a game object using instantiate. 1 Answer

Referencing booleans from settings 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