Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bdollmont · Oct 08, 2015 at 04:24 AM · c#prefabinheritance

Why do two different prefabs with two different scripts take each other's variables?

So, bit of a long one, here.

I've been trying to learn Unity through doing, mostly. I have been building a little platformer, and trying to make it semi-randomized, by providing small-ish chunks of maps as prefabs, then organizing them into a line to create one long chain of them. The issue started with having more than one.

In order to randomize it a bit more, I'm feeding a List of positions into each prefab, first through the Inspector as the Lists were public, and now through code as that wasn't working. The problem is that, occasionally, one prefab will take coordinates from the other prefab's List, and vice versa. I'm still unfamiliar with Unity and C# in general, so I'm really not certain why this is happening.

Here's an example of one of the scripts I'm using:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Random = UnityEngine.Random;
 
 public class BoardOne: MonoBehaviour {
     
     private List <Vector2> enemyPositions = new List<Vector2> ();
     private List <Vector2> coinPositions = new List<Vector2> ();
     private List <Vector2> boxPositions = new List<Vector2> ();
     private List <Vector2> grassPositions = new List<Vector2> ();
     
     public GameObject[] enemy;
     public GameObject coin;
     public GameObject box;
     public GameObject grass;
     
     private int enemyMax = (Random.Range (1,3));
     private int coinMax = (Random.Range (2,5));
     private int grassMax = (Random.Range (0,2));
     
     private Transform itemHolder;
     
     // Use this for initialization
     void Awake () 
     {
         enemyPositions.Add (new Vector2(4.8f, 4.55f));
         enemyPositions.Add (new Vector2(14.4f, 2.95f));
         enemyPositions.Add (new Vector2(12.8f, -0.25f));
         enemyPositions.Add (new Vector2(6.4f, -0.25f));
 
         coinPositions.Add (new Vector2(6.4f, 4.8f));
         coinPositions.Add (new Vector2(14.4f, 3.2f));
         coinPositions.Add (new Vector2(9.6f, 1.6f));
         coinPositions.Add (new Vector2(6.4f, 1.6f));
         coinPositions.Add (new Vector2(11.2f, 1.6f));
         coinPositions.Add (new Vector2(12.8f, 3.2f));
 
         boxPositions.Add (new Vector2(17.6f, -0.43f));
         boxPositions.Add (new Vector2(3.2f, -0.43f));
         boxPositions.Add (new Vector2(3.2f, 4.37f));
 
         grassPositions.Add (new Vector2(6.4f, -1f));
         grassPositions.Add (new Vector2(14.4f, -1f));
         grassPositions.Add (new Vector2(4.8f, 3.8f));
 
         ItemGenerate ();
     }
     
     void ItemGenerate()
     {
         itemHolder = new GameObject ("Item").transform;
         
         for (int x = 0; x < enemyMax + 1; x++)
         {
             int randomIndex = Random.Range (0, enemyPositions.Count);
             Vector2 randomPosition = enemyPositions[randomIndex];
             enemyPositions.RemoveAt (randomIndex);
             randomPosition.x += (GameManager.boardCount * 22.4f);
             GameObject enemyChoice = enemy[Random.Range (0, enemy.Length)];
             GameObject instance = Instantiate (enemyChoice, randomPosition, Quaternion.identity) as GameObject;
             instance.transform.SetParent (itemHolder);
         }
         
         for (int x = 0; x < coinMax + 1; x++)
         {
             int randomIndex = Random.Range (0, coinPositions.Count);
             Vector2 randomPosition = coinPositions[randomIndex];
             coinPositions.RemoveAt (randomIndex);
             randomPosition.x += (GameManager.boardCount * 22.4f);
             GameObject instance = Instantiate (coin, randomPosition, Quaternion.identity) as GameObject;
             instance.transform.SetParent (itemHolder);
         }
         
         for (int x = 0; x < grassMax + 1; x++)
         {
             int randomIndex = Random.Range (0, grassPositions.Count);
             Vector2 randomPosition = grassPositions[randomIndex];
             grassPositions.RemoveAt (randomIndex);
             randomPosition.x += (GameManager.boardCount * 22.4f);
             GameObject instance = Instantiate (grass, randomPosition, Quaternion.identity) as GameObject;
             instance.transform.SetParent (itemHolder);
         }
         
         for (int x = 0; x < 1; x++)
         {
             int randomIndex = Random.Range (0, boxPositions.Count);
             Vector2 randomPosition = boxPositions[randomIndex];
             boxPositions.RemoveAt (randomIndex);
             randomPosition.x += (GameManager.boardCount * 22.4f);
             GameObject instance = Instantiate (box, randomPosition, Quaternion.identity) as GameObject;
             instance.transform.SetParent (itemHolder);
         }
     }
 
 
 
     // Update is called once per frame
     void Update () {
         
     }
 }

Any thoughts? Or any suggestions in general? Still learning, here, and always happy to have help.

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

0 Replies

· Add your reply
  • Sort: 

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

33 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 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

Locate _MainTex from a public shader and assign to instantiated prefab for GUI 1 Answer

GameObject positions not lining up 0 Answers

Spawn in a different and random Spawn point from a list 0 Answers

Script updates prefab text 0 Answers

2D Shooter Make Item Pickup Change Bullet Prefab On Collision 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