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 Satamanster · Jul 09, 2012 at 08:07 PM · prefabvariablecomponentinstance

Updating variable on another script. It is not working.

Ok so i have my character going around and he his supposed to get life whenever he hits a Tool Tool Box. Anyway, i'm trying to display my character life but something it isn't right...

In the tool box i have this script called TesteTool.js:

 > #pragma strict
 > 
 > var vida = 50;
 > 
 > function Start() { Debug.Log(vida); }
 > 
 > function OnTriggerEnter (other :
 > Collider) {
 > 
 > if(vida<=80) { vida = vida+20;
 > Debug.Log(vida); } else {
 > vida=100; Debug.Log(vida); }
 >     Destroy(gameObject); }

P.S. - Vida means life in Portuguese.

Well i know life was supposed to be attached to the character but it doesn't matter in this case. Well on the other hand i have a script attached to the camera with this:

 #pragma strict
 @script ExecuteInEditMode()
 
 var vidaOnGui;
 
 var width = 70;
 var height = 25;
 
 function Update()
 {
  vidaOnGui=GameObject.Find("ToolBox").GetComponent(TesteTool).vida;
 }
 function OnGUI()
 
 {
      GUI.Box(Rect(0,0,width,height),"Vida:" +vidaOnGui);
 }

Well now everything works fine and when i press play i get the life showing on my left corner, hmmm, almost. When i go into the tool box i get a NullReferenceException error. Why is that and how can i fix it?

Oh and by the way, just for curiosity, but what happens if i have more than one object with the same name in the hierarchy? I mean, when i ask to get the component of the object with that name? And this is not always fixable, because, in example, when you instantiate prefabs along the scene they all get the same name. Am i missing something here?

Glad to be among such nice people like you guys. Please help me. ;) Thanks.

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 Satamanster · Jul 09, 2012 at 08:18 PM 0
Share

I don't know if it helps but debug.logging the life returns that when i enter the trigger the life keeps updating +20 forever...

2 Replies

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

Answer by fafase · Jul 09, 2012 at 08:24 PM

Try to use static typing:

var some:int;

your var videOnGui is not typed.

weirdly pragma strict should not let you go.

Try to use :

 var vidaOnGui:int;


The other one works because you provide an additional info:

 var some=10;

Unity sees 10 and knows it is an int.

Edit: Try this:

 #pragma strict
 @script ExecuteInEditMode()
 
 var vidaOnGui:TesteTool;
 
 var width = 70;
 var height = 25;
 
 function Start()
 {
  vidaOnGui=GameObject.Find("ToolBox").GetComponent(TesteTool);
 }
 function OnGUI()
 {
      var laVida=vidaOnGui.vida;
      GUI.Box(Rect(0,0,width,height),"Vida:" + laVida);
 }
 
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 Satamanster · Jul 09, 2012 at 08:33 PM 0
Share

Ok, thanks a lot, now the error is gone, but still the life isn't being updated on the gui box, it does in the console but not int the box. Can you help me on that aswell? Don't worry, when my question is fully filled i'll mark your answer as correct. Just wan't to ask you aswell to vote up in my question just for the matter that i'm doing a work for college in unity and since i'm new here, i have to wait for my questions to be accepted in the queue before being submitted, would you help me out getting those 15 points by voting in my question? $$anonymous$$ost appreciated. :D Thanks a lot.

avatar image Satamanster · Jul 09, 2012 at 08:57 PM 0
Share

Hmmm. Your code worked, i do understand it, i just don't get why does that work and $$anonymous$$e doesn't. if you can explain me that it would be great. Now just one left thing to go... Remember that question i made at last in my post... Well it matters here because, imagine, if i duplicate my toolbox now, and you press play. Imagine you life starts at 50, after i hit the first toolbox it updates to 70, but after you hit a second one, nothing occurs. Do you have a solution for me please? :D Thanks again.

avatar image
0

Answer by Satamanster · Jul 10, 2012 at 12:56 AM

Let it be, i just figured it out. Actually, i deleted almost everything and did it the opposite way, i was being so stupid doing things this way, so i tagged my toolbox prefab and then putted them wherever i wanted. Then attached the script to my character(is a tank), and set it to display the life in the update of that character... Solve 3 problems in one script... eheheh, Thanks a lot for the help fafase. :D

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent returns only the last instance 1 Answer

Is it possible to set up a prefab variable such that it can only be modified on the prefab and not on the instance? 3 Answers

increase a variable of a component script 4 Answers

Unity setting SerializedProperty.prefabOverride incorrectly 2 Answers

Updating a variable on a script in an instanced object 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