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 awplays49 · Apr 01, 2015 at 04:01 PM · 2dcolorcubetreerandomize

Tree spawn script not working

Hi, these are some script I've been working on. They are giving me quite a hard time :(

 using UnityEngine;
 using System.Collections;
 
 public class Sapling : MonoBehaviour {
 
     private int WaitTime;
     public int MinWaitTime;
     public int MaxWaitTime;
     private float TimeWaited;
     private bool HasGrown;
     public GameObject CubeGet;
     public Sprite [] Sprites = new Sprite [2];
     
     void Start () {
         WaitTime = Random.Range (MinWaitTime, MaxWaitTime + 1);
     }
     
     void Update () {
         if (TimeWaited < WaitTime)
         {
             TimeWaited += 1 * Time.deltaTime;
         }
         else
         {
             if (HasGrown == false)
             {
                 Grow ();
                 HasGrown = true;
             }
         }
     }
     void Grow () {
         int LogHeight = Random.Range (1, 5);
         for (int LogsSpawned = 0; LogsSpawned < LogHeight + 1; LogsSpawned ++)
         {
             GameObject CurrentLog = Instantiate (CubeGet, transform.position + Vector3.up * LogsSpawned, Quaternion.identity) as GameObject;
             CurrentLog.GetComponent <Cube> ().Type = Cube.Types.Wood;
         }
         int LeafHeight = Random.Range (2, 4);
         GameObject CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 1) + Vector3.left, Quaternion.identity) as GameObject;
         CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
         CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 1), Quaternion.identity) as GameObject;
         CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
         CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 1) + Vector3.right, Quaternion.identity) as GameObject;
         CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
         CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 2), Quaternion.identity) as GameObject;
         CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
         if (LeafHeight == 2)
         {
             Destroy (gameObject);
         }
         if (LeafHeight == 3)
         {
             CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 2) + Vector3.left, Quaternion.identity) as GameObject;
             CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
             CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 2) + Vector3.right, Quaternion.identity) as GameObject;
             CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
             CurrentLeaf = Instantiate (CubeGet, transform.position + Vector3.up * (LogHeight + 3), Quaternion.identity) as GameObject;
             CurrentLeaf.GetComponent <Cube> ().Type = Cube.Types.Leaf;
             Destroy (gameObject);
         }
     }
 }

This ^ is a sapling script, that spawns one of several presets of trees after the timer passes a random time.

I think the way I wrote the leaf spawn part may be related to the fact that I can't inhabit leaves. In my game, you can play as any block by clicking on that block. The leaf blocks can be inhabited, but the color is not right. When I click on a leaf block, It becomes black, instead of getting a tint of red, which is what I wanted.

The wood blocks work fine, however, which is why I find this very weird.

I did in fact check if the block wasn't being assigned the enum, but it was. "Type" is "Types.Leaf", yet my code doesn't tint the leaves red when clicked on.

In fact, in the inspector that item still says its the previous color until i move the block, and then it says it's red. However, it is not red. It is black.

Here's the cube script:

 using UnityEngine;
 using System.Collections;
 
 public class Cube : MonoBehaviour {
 
     public GameObject [] Managers;
     public int DataValue;
     public enum Types {
         Cube,
         Wood,
         Leaf
     };
     public Types Type;
     public GameObject ButtonGet;
     private bool IsMoving;
     private bool MouseOver;
     public Sprite [] Sprites = new Sprite [3];
 
     void Start () {
         Managers [0].GetComponent <CubeManager> ().Cubes.Add (gameObject);
         DataValue = Managers [0].GetComponent <CubeManager> ().Cubes.Count - 1;
         if (Type == Types.Cube)
         {
             if (DataValue == 0)
             {
                 GetComponent <SpriteRenderer> ().sortingOrder = 20;
                 GetComponent <SpriteRenderer> ().color = Color.red;
             }
             else
             {
                 GetComponent <SpriteRenderer> ().sortingOrder = 0;
                 GetComponent <SpriteRenderer> ().color = Color.black;
             }
         }
         else
         {
             GetComponent <SpriteRenderer> ().color = Color.white;
         }
         if (Type == Types.Wood)
         {
             GetComponent <SpriteRenderer> ().sortingOrder = 1;
             GetComponent <SpriteRenderer> ().sprite = Sprites [1];
         }
         if (Type == Types.Leaf)
         {
             GetComponent <SpriteRenderer> ().sortingOrder = 2;
             GetComponent <SpriteRenderer> ().sprite = Sprites [2];
         }
         gameObject.name = Type + " (" + DataValue + ")";
     }
 
     void Update () {
         if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D)) 
         {
             if (IsMoving == false && Managers [0].GetComponent <CubeManager> ().CurrentCube == DataValue) 
             {
                 StartCoroutine (Move ());
             }
         }
         else
         {
             StopCoroutine (Move ());
         }
         if (MouseOver == true && Input.GetMouseButton (1) && Managers [0].GetComponent <CubeManager> ().CurrentCube != DataValue)
         {
             Destroy (gameObject);
         }
     }
 
     IEnumerator Move () {
         IsMoving = true;
         if (IsMoving == true)
         {
             if (Input.GetKey (KeyCode.W))
             {
                 transform.position += Vector3.up;
                 if (Camera.main.transform.position.y < transform.position.y - 3)
                 {
                     Camera.main.transform.position += Vector3.up;
                 }
             }
             if (Input.GetKey (KeyCode.A))
             {
                 transform.position += Vector3.left;
                 if (Camera.main.transform.position.x > transform.position.x + 5)
                 {
                     Camera.main.transform.position += Vector3.left;
                 }
             }
             if (Input.GetKey (KeyCode.S))
             {
                 transform.position += Vector3.down;
                 if (Camera.main.transform.position.y > transform.position.y + 3)
                 {
                     Camera.main.transform.position += Vector3.down;
                 }
             }
             if (Input.GetKey (KeyCode.D))
             {
                 transform.position += Vector3.right;
                 if (Camera.main.transform.position.x < transform.position.x - 5)
                 {
                     Camera.main.transform.position += Vector3.right;
                 }
             }
         }
         yield return new WaitForSeconds (0.16f);
         IsMoving = false;
     }
     
     void OnMouseOver () {
         MouseOver = true;
     }
     
     void OnMouseExit () {
         MouseOver = false;
     }
     
     void OnMouseDown () {
         if (Managers [0].GetComponent <CubeManager> ().Cubes [Managers [0].GetComponent <CubeManager> ().CurrentCube].GetComponent <Cube> ().Type == Types.Cube)
         {
             Debug.Log ("Black");
             Managers [0].GetComponent <CubeManager> ().Cubes [Managers [0].GetComponent <CubeManager> ().CurrentCube].GetComponent <SpriteRenderer> ().color = Color.black; 
         }
         else
         {
             Managers [0].GetComponent <CubeManager> ().Cubes [Managers [0].GetComponent <CubeManager> ().CurrentCube].GetComponent <SpriteRenderer> ().color = Color.white; 
         }
         Managers [0].GetComponent <CubeManager> ().CurrentCube = DataValue;
         GetComponent <SpriteRenderer> ().color = Color.red;
         
     }
 }
 

Also all Cube Manager is is just a container for all cubes. It hold a generic list for all blocks, knows the current cube's data value, etc. However, it has nothing to do with this.

To prove it, here is the CubeManager script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CubeManager : MonoBehaviour {
 
     public GameObject CubeGet;
     public List <GameObject> Cubes = new List <GameObject> ();
     public int CurrentCube;
     public int MinCubes;
     public int MaxCubes;
     public int MinX;
     public int MaxX;
     public int MinY;
     public int MaxY;
 
     void Start () {
         int CubesToSpawn = Random.Range (MinCubes, MaxCubes + 1);
         for (int CubesSpawned = 0; CubesSpawned < CubesToSpawn + 1; CubesSpawned ++) 
         {
             Instantiate (CubeGet, new Vector2 (Random.Range (MinX, MaxX + 1), Random.Range (MinY, MaxY + 1)), Quaternion.identity);
         }
     }
 }
 

I also already checked if I assigned the wrong thing, but I wouldn't have because I would've gotten a null reference exception if I had assigned the wrong manager.

I hope the community can help me, thank you very much to all of you in advance.

Comment
Add comment · Show 1
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 awplays49 · Apr 01, 2015 at 05:20 PM 0
Share

Guys I know this is a long question but It's not as bad as you think :-)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Navigatron · Apr 01, 2015 at 10:19 PM

I'm not quite sure exactly what you are asking, but I can help a bit with the color problem. When placing a red filter over a green block, the red filter only lets through red light. The green block does not have any red light, its green. The green light can't go through the filter, and because there is no red light, the block appears black. The easiest way to fix this (if you wrote your own code for the color changing) is to allow some green and blue light through. instead of multiplying the color value by (1,0,0) try something like (1,0.6,0.6). Then you will probably see something more like this instead of black.

alt text

Good luck.

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

20 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

Related Questions

How do I make a grey cube transparent? 1 Answer

Incorrect Sprite Color on Android build 0 Answers

How to render a colored 2D rectangle. 6 Answers

Is there a way to drag a public color into another object's color field? 1 Answer

On Touch change color 2 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