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 littlepsycho · Nov 24, 2013 at 01:50 PM · javascriptprefabvariable

Variables are getting permanently stored in prefabs

Hello!

I have a problem with prefabs. Before describing my actual problem here is the code that is causing my problem:


RessourceScript.js

 #pragma strict
 
 var gold : int = 0;
 var wood : int = 0;
 var stone : int = 0;
 var clay : int = 0;
 var iron : int = 0;
 var sulphur : int = 0;
 var silver : int = 0;
 var bronze : int = 0;
 
 
 function Costs(goldCost : int, woodCost : int, stoneCost : int, clayCost : int, ironCost : int, sulphurCost : int, silverCost : int, bronzeCost : int) {
     if (
         gold >= goldCost && 
         wood >= woodCost && 
         stone >= stoneCost && 
         clay >= clayCost && 
         iron >= ironCost && 
         sulphur >= sulphurCost && 
         silver >= silverCost && 
         bronze >= bronzeCost
         ) 
     {
         gold -= goldCost;
         wood -= woodCost;
         stone -= stoneCost;
         clay -= clayCost;
         iron -= ironCost;
         sulphur -= sulphurCost;
         silver -= silverCost;
         bronze -= bronzeCost;
         return true;
     } else {
         return false;
     }
 }



BuildingCosts.js

 #pragma strict
 
 var goldCost : int = 0;
 var woodCost : int = 0;
 var stoneCost : int = 0;
 var clayCost : int = 0;
 var ironCost : int = 0;
 var sulphurCost : int = 0;
 var silverCost : int = 0;
 var bronzeCost : int = 0;
 var textures : Texture[];
 var cameraObject : Camera;
 @HideInInspector
 var ressource : RessourceScript;
 
 function Build(position : Vector3) {
     ressource = cameraObject.GetComponent(RessourceScript);
     if (ressource.Costs(goldCost,woodCost,stoneCost,clayCost,ironCost,sulphurCost,silverCost,bronzeCost))
     {
         var last = Instantiate (gameObject, Vector3(Mathf.Round(position.x),0,Mathf.Round(position.z)), Quaternion.identity);
         var tex = Random.Range(0, textures.length);
         last.renderer.material.SetTexture("_MainTex", textures[tex]);        
     } else {
         Debug.Log("Failed");
     }
 }



The BuildingCosts script is attached to every building prefab. (Costs for every building set in the inspector) The RessourceScript is attached to the MainCamera.

My main problem right now is, that as soon as I call buildingScript.Build(Vector3 from a raycast here); it changes the gold variable in the Camera prefab, not the camera instance in the scene.

This one minute long YouTube video briefly shows my problem: http://www.youtube.com/watch?v=offaB2d0yvg

(Watch the game windows and the inspector). I think that this is happening because the MainCamera is a prefab, but I havent known that a script can "edit" a prefab. How can I work around this?

Thank you!

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
0
Best Answer

Answer by Durayel · Nov 24, 2013 at 03:56 PM

Rather than trying to set the camera in the inspector for every prefab, ensure that your camera object/prefab has the tag "MainCamera" and then just use

resource = camera.main.GetComponent(ResourceScript);

Comment
Add comment · Show 1 · 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 littlepsycho · Nov 24, 2013 at 04:25 PM 0
Share

Perfect! Exactly what I needed. Thank you! :)

avatar image
0

Answer by Bunny83 · Nov 24, 2013 at 03:16 PM

That simply means your "cameraObject" variable doesn't reference the instance in the scene but the camera prefab. That's the only possible reason ;)

Comment
Add comment · Show 3 · 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 littlepsycho · Nov 24, 2013 at 03:42 PM 0
Share

The problem is that I cant drag the Camera instance in the scene onto the cameraObject variable. It only allows me to use prefabs for that.

avatar image Bunny83 · Nov 24, 2013 at 03:43 PM 0
Share

To me it looks like you're executing all your code on the prefabs and not on instances. A prefab can never reference an object in the scene.

Where do you call the Build method and on what object?

avatar image littlepsycho · Nov 24, 2013 at 04:03 PM 0
Share

The Build method is being called from the BuildingScript, which is also attached to the $$anonymous$$ainCamera prefab:

 #pragma strict
 
 var buildings : GameObject[];     // An Array with all building GameObjects
 @HideInInspector
 var activeBuilding : int = 0;    // The Building were going to build
 
 
 function Start () {
     Debug.Log(buildings[0]);Debug.Log(buildings.length);
 }
 
 function Update () {  
     if (Input.Get$$anonymous$$ouseButtonDown(0)){ 
         var ray = camera.ScreenPointToRay(Input.mousePosition);
         var hit: RaycastHit;
         if (Physics.Raycast(ray, hit)){ 
         
             Debug.Log(buildings[activeBuilding]);
             var buildingScript = buildings[activeBuilding].GetComponent(BuildingCosts);
             buildingScript.Build(hit.point); // <-- !! !! !! !!
         }
     }
 }
 
 function OnGUI () {
     if (GUI.Button (Rect (Screen.width-130,Screen.height-50,120,40), "Building")) {
         if (activeBuilding < buildings.Length-1) 
             {
                 activeBuilding++;
                 Debug.Log(activeBuilding);
             }
         else 
             {
                 activeBuilding=0;
                 Debug.Log(activeBuilding);
             }
     }
     GUI.Label(Rect (10,Screen.height-50,200,40), ""+buildings[activeBuilding]);
 }

"To me it looks like you're executing all your code on the prefabs and not on instances. A prefab can never reference an object in the scene."

But I dont have the GameObjects I want to Instantiate in the scene. The script instantiates prefabs(Like a Tower), but these prefabs need references to objects in the scene(Like my $$anonymous$$ainCamera, which holds the information about the ressources).

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

18 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

Related Questions

Change Variable on Another Script 2 Answers

[Urgent]Changing variables of an object AFTER instantiation 2 Answers

Prefabs and variables 1 Answer

How I can reuse a bullet prefab whith enemies and my character? 1 Answer

Changing Variable OnMouseClick? Help! 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