• 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 1ArmedScissor · Apr 23, 2015 at 05:52 PM · variablesinstances

Instantiated variables of a prefab + script

Hi, total noob here, sorry for not being able to make sense out of answers to similar questions here.

I'm instantiating a tile, which is a prefab sprite with a script component and some text. The text shows the value of a variable of the script.

As I've instantiated the whole thing, I now have a bunch of tiles, each with their own text.

So far so good. However, all of these variables are the same actual variable. I can't figure out how to instantiate each script's variable.

for each tile i'm trying this:

 TileArray[i].GetComponent<tiledata>().TileValue += 1;
 text.text = "" +TileArray[i].GetComponent<tiledata>().TileValue;

Instead of having each individual tile get +1, it's adding +1 to the same instance of the variable. Tile 1 value = 1 Tile 2 value = 2 etc.

What I want is to have each for loop add +1 to an instantiated variable, resulting in: Tile 1 value = 1 Tile 2 value = 1

Hope any of this makes sense... Cheers guys!

Comment
Add comment · Show 7
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 _creatio_ · Apr 23, 2015 at 06:06 PM 0
Share

Just a blind guess, can't your property TileValue access a static variable?

avatar image 1ArmedScissor · Apr 24, 2015 at 07:32 AM 0
Share

I tried it both as a static var and not. Using a static var would not compile for me. I'll post the whole script later...

avatar image JinJin · Apr 24, 2015 at 08:33 AM 0
Share

can you post both scripts: the tiledata script and the script that is using it?

avatar image 1ArmedScissor · Apr 24, 2015 at 07:41 PM 0
Share

tiledata script:

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 
 public class tiledata : $$anonymous$$onoBehaviour {
 
     public int TileValue;
 
     // Use this for initialization
     void Awake () 
     {
         TileValue = 10;
     }

}

The script calling up tiledata:

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
 
     public class hostiles : $$anonymous$$onoBehaviour {
 
     public GameObject[] TileArray;
 
     Text text;
 
     void HostileSpawner () 
     {
         TileArray = GameObject.FindGameObjectsWithTag ("Tile") ;
                 
         for (int i = 0; i < TileArray.Length; i++) 
         {
             print ("Tile " +i+ " Position:" +TileArray[i].transform.position+ " Tile Value " +TileArray[i].GetComponent<tiledata>().TileValue);
 
             TileArray[i].GetComponent<tiledata>().TileValue +=1;
 
             text.text = "" +TileArray[i].GetComponent<tiledata>().TileValue;
 
         }
 }
 
     void Start () 
     {
         text = GetComponent <Text> ();
         InvokeRepeating ("HostileSpawner", 0, 2);
     }
 }






avatar image JinJin · Apr 25, 2015 at 08:45 AM 0
Share

can you change your function to this:

  void HostileSpawner () 
  {
      TileArray = GameObject.FindGameObjectsWithTag ("Tile") ;
              
      for (int i = 0; i < TileArray.Length; i++) 
      {
          tiledata data = TileArray[i].GetComponent<tiledata>();
              
          print ("Tile " + i + " Position:" + TileArray[i].transform.position + " Tile Value " + data.TileValue);
  
          data.TileValue += 1;
  
          text.text = data.TileValue.ToString();
  
          print (data.TileValue);
      }
 }

and post what is printed to your unity console?

avatar image Fir3stormStudio · Apr 25, 2015 at 09:15 AM 0
Share

if you just need a tile data in that increases each time a tile is spawned couldnt you have the tiledata vallued stored on a seperate script like a game manager script then for the title data script have an int called thisTileValue then find the game manager script the have the tiledata value then use

 gamemanger.titledata = gamemanger.titledata + 1; 
 thisTileValue = gamemanger.tiledata;
avatar image Fir3stormStudio · Apr 25, 2015 at 09:23 AM 0
Share

here is an example for you I have a game object called gm with a script called gm on it using UnityEngine; using System.Collections;

 public class gm : $$anonymous$$onoBehaviour {
     public int tileValue;
 
     void Start () 
     {
         tileValue = 10;    
     }
 }

I then have another game object called tile with the following script

 using UnityEngine;
 using System.Collections;
 
 public class tilevalue : $$anonymous$$onoBehaviour {
     public gm gm;
     public int thisTileValue;
     void Start () 
     {
         gm = GameObject.Find ("gm").GetComponent<gm>();
         gm.tileValue = gm.tileValue + 1;
         thisTileValue = gm.tileValue;
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fappp · Apr 25, 2015 at 09:30 AM

Add an ID variable to your tileData script, and only assign +1 if the ID equals! Adding ID's to objects that are instantiated multiple times is always a good idea!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem storing GameObject reference in script - PUN 0 Answers

Store different int values in an instantiated prefab 1 Answer

Accessing variables from GameObjects in an array 3 Answers

Calling a mesh instance 'mesh' or 'msh' (to avoid the keyword) 1 Answer

Create new variable name based on array index 3 Answers


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