• 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 Wakeful · Jan 20, 2015 at 02:37 AM · c#editorprefabvariablegetcomponent

Changing Prefab Variables From Another Script In Editor

Hey,

I have a prefab with script X on it variable XX. I also have a script Z with the variable ZZ on another object(not the prefab). While in the editor I want to be able to change variable XX to ZZ. So what ever ZZ is XX always equals on the prefab. I also want to use script X on more than one prefab with different XX values. If im not clear about what im asking please let me know so I can clarify...Thx

~Wakeful

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 fddefdef · Jan 20, 2015 at 07:30 AM 0
Share

So you want to access script X which is on a prefab from a script attached to an object in the scene?

avatar image Wakeful · Jan 20, 2015 at 04:14 PM 0
Share

Yes that is correct. Script X is on a prefab and Script Z is on a gameObject in the scene.

1 Reply

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

Answer by abi-kr01 · Jan 20, 2015 at 05:12 PM

Here is something that i have done for my projects it might help you as well

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Z : MonoBehaviour {
      public int zz; //varible   zz on main script
 
     void Start () 
     {
         update_value ();
     }
     
  
     void update_value () 
     {
         //finding all gameobjects that are avail on scene 
         foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
         {
             zz=Random.Range(0,100);    
             if (obj.transform.tag == "prefab")  // finding and adding new value in xx with new zz for each gameobjects with prefab tag 
             {
                 print("Before update "+obj.GetComponent<X>().xx);
                 obj.GetComponent<X>().xx=zz;
                 print("After Update"+obj.GetComponent<X>().xx);
             }
         }
     }
 }

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 abi-kr01 · Jan 20, 2015 at 05:13 PM 0
Share

here is the script that is attach to all prefab

 using UnityEngine;
 using System.Collections;
 
 public class X : MonoBehaviour {
 
     public int xx;
     
      
 }

avatar image Wakeful · Jan 21, 2015 at 05:33 AM 0
Share

Close but not what im looking for, but thanks tho. I want to be able to update the prefab before I run the game. What im doing is only going to be for when im in the editor anyways, but it would make it a lot easier for me when Im creating alot of prefabs and all have then in a list.

avatar image abi-kr01 · Jan 21, 2015 at 07:13 AM 0
Share

the "Z" script is of type "mono behavior" you can use "EditorWindow" and modify script to do the same task

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Serialize problem with Unity Editor, values reset on game execution! 1 Answer

Clone of prefabs copy original´s script state during runtime? 0 Answers

Referencing in a prefab 1 Answer

creating a assignable asset 1 Answer

Initialising List array for use in a custom Editor 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges