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 mteom · Jan 03, 2015 at 06:07 PM · c#prefabsreference

Prefab, referencing them, using their methods:bug or intentional?

Hello everybody. Sorry for spamming with questions, but it seems i can't grasp how to reference objects, particularly prefabs. I will try to describe the situation at my best. I have 2 objects, both prefabs (Player and Rock). Attached to "Player" there is a script called "player":

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public int lives = 3;       //default number of lives
     public int keys = 0;        //default number of keys
     public int score = 0;       //default score
     public bool isDead = false; //default dead status
     public int weapontype = 1;  //default weapon type
     public int power=4;
 
     // Use this for initialization
     void Start()
     {
         power = 4;
     }
 
     // Update is called once per frame
     void setPower()
     {
         power = 4;
     }
 
     public void changePower()
     {
         power--;
     }
 
     void Update()
     {
         Debug.Log(power);
     }
 }


Attached to Rock there is a script called "ChangePower":

 using UnityEngine;
 using System.Collections;
 
 public class ChangePower : MonoBehaviour
 {
     public Player _player;
     // Use this for initialization
     void Start()
     {
         Player _player = GetComponent<Player>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             _player.changePower();
         }
     }
 }

In the Inspector, i "drag reference" both player and rock into the respective "scripts" slots, so i can see that i have created the reference (the inspector let me create prefabs references only with other prefabs):i then drag the prefabs into the scene, then start the game...the variable "power" isn't getting updated, like the objects aren't active. Now...if after placing the prefabs in the scene (from the prefab folder) i create the references for both scripts using the objects from the scene (and NOT from the prefab folder) the variable "power" is updated properly...like the objects are now active. Is it supposed to be like that or i don't understand how to use prefabs properly(and reference them through scripts)? Hopefully i have explained the situation in a clear way. I also got a feeling that this is the same problem i was having before in my previous posts...not sure why! 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

1 Reply

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

Answer by NoseKills · Jan 03, 2015 at 11:23 PM

both player and rock into the respective "scripts" slots,

I don't know if I understand completely since I can't see anything that could be dragged into Player script to be assigned. On ChangePower there is a Player variable that can be assigned by dragging.

But if I understand the rest correctly what you describe is expected behavior. If you drag a prefab from the "Project" view into a "slot" (variable) on another prefab/script, that reference really points to the prefab in the Project view. That's what is happening in the first example you give. Even when you drag the prefabs into the scene, the dragged references in the scripts of the prefabs in the scene still point to the objects in project view. You can then use those references to make new objects (Instantiate new prefabs from project view into the scene) in your game.

In your second example you already Instantiate the prefabs by dragging them from project view into the scene. Then you make the references in the scene, so the references point to the actual "live" objects in the scene, and it all works as you'd expect.

The prefabs in project view are meant for re-using and if things would work as you expect in your first attempt, things would get really difficult if you were to drag more and a different number of players and rocks into the scene. Let's say you drag 2 "rocks" and 3 "player"s into the scene after making the references in project view. Which Player object would each rock point to now ? What if you'd only drag rocks into the scene...which players would the rocks reference then when there are none in the scene...

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 mteom · Jan 04, 2015 at 12:20 PM 0
Share

Thank you, that make sense. I found a way to pick the exact object i needed and then reference it; this way i can have multiple copies of the prefab that don't get into each other way since the "active" object can only be one. Thanks.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

[SOLVED] Prefab as copy not as reference 1 Answer

Distribute terrain in zones 3 Answers

Do you have to set a object reference for every script? 0 Answers

instantiate prefabs y+1 from last instintiated prefab 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