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
1
Question by Karsnen_2 · Dec 09, 2011 at 05:26 AM · javascriptgameobjectprefab

Prints correct on console but not updating on Prefab/Gameobject.

Hello,

I have a prefab which I am using in the game as the enemy. Now - I have a variable attach to that which transforms when it gets a hit. This variable is accessed by an empty gameobject which is responsible to display it using OnGUI() function.

Code:

 var r_enepos : float;
 var e_dis : float;
 var e_distance : float;
 function OnCollisionEnter(hit : Collision)
 {
    randomrange();
    if(hit.gameObject.tag == "cannonball")
    {
           transform.position = Vector3(r_enepos,2,0);
    }
 }
 function Update()
 {
     e_distance = gameObject.transform.position.x + 21; 
     Debug.Log("enemy distance" + e_distance);
     if(gameObject.transform.position.z!=0)
     gameObject.transform.position.z=0;
 }
 
            
 function randomrange()
 {
     if(Random.Range(0,10)<7)
     r_enepos =  Random.Range(-18,10);
     else
     r_enepos = Random.Range(10,21);
 }         
 



Code Explanation:

If you have to look into the code, e_distance is the variable which is inherited in another emptygameobject in the OnGUI() function.

GameObjects:

This script is attached to the "enemy" prefab and that prefab is drag onto the empty gameobject which is referencing this script. To be clear again one is a Prefab and other is an gameobject on the scene view.

Problem:

As you might see on the code, I make an attempt to print it on the screen. When I run the level, the value that prints on the console is accurate but on the GUI , it is just zero (which is the initial value). Moreover when I did trace a bit - I happen to find that the variable e_distance on the inspecter pane of "enemy" prefab remains zero. i.e. the value in the prefab does not change.

Request:

I am not sure where I have went wrong. I would be happy and would appreciate if someone could help me on this.

THANK YOU in advance.

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

Answer by syclamoth · Dec 09, 2011 at 07:00 AM

The problem is that while the enemy instance changes, the value in the prefab does not. In fact, the prefab doesn't even execute 'Update', so you're getting the GUI value out of one object, and the Debug.Log value from a totally different object which doesn't even know about the prefab it was instantiated from.

Basically, when you instantiate the prefab (in the usual way), you need to pass a reference to the instance that was created to the GUI object that keeps track of it. The prefab never moves, because it is a 'virtual' object that can't act on the game world without first being instantiated, but on the other hand can never be modified or destroyed (and so is sure to be a predictable starting point for other objects). Think about it the same way you would the relationship between a class and an object of that class.

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 Karsnen_2 · Dec 09, 2011 at 07:05 AM 0
Share

So Am i supposed to pass a reference of the Instantiated prefab?

Hmmmm.

avatar image Karsnen_2 · Dec 09, 2011 at 07:50 AM 0
Share

Could you please help me with some code? I can't figure it out.

avatar image RodrigoLins · May 27, 2012 at 03:59 PM 0
Share

I have the same problem I couldn't find a answer yet... I don't get why people knows how to solve the problem, but doesn't help with the code... I see this happenning all the time overhere... sad...

avatar image
1

Answer by ArunChnadran · Mar 20, 2012 at 02:42 PM

just Instantiate another variable of type gameobject. for eg:-

 var enemy: GameObject;  //(can be declared as an array, if you want)

then Instantiate the prefab to it.

 enemy = Instantiate (yourPrefabName, vector(0,0,0), Quaternion. Identity);

now, you can acces the variable from these game objects.

 enemy.GetComponent(yourCodename).yourVariable;

I hope you have got this answer long before, but I am writing this down coz the anyone with same problem is supposed to end here. Syclamoth has given the answer but a beginner like me took a lot of time to grasp what he said. So I am answering here even though its late.

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

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

Setting Scroll View Width GUILayout 1 Answer

Player 1 and Player 2 move together 1 Answer

Position of a GameObject 2 Answers

Destroying a prefab on collision with a cube? 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