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
1
Question by Zyklon · Dec 05, 2011 at 12:47 PM · camerabackground

Changing camera background color in runtime

Hello!

I tried to make a simple scene and change the camera's background color.

My script:

using UnityEngine; using System.Collections;

public class MainMenuItem : MonoBehaviour { private bool newGame; public Color color1; public Color color2; public float duration;

 private Camera cam;
 
 void Awake() 
 {
     cam = Camera.mainCamera.camera;
 }
 
 void Start()
 {
     newGame = false;
     color1 = Color.black;
     color2 = Color.white;
     duration = 3.0f;
 }
 
 void Update()
 {
     if(newGame)
     {
         float t = Mathf.PingPong(Time.time, duration) / duration;
         cam.backgroundColor = Color.Lerp(color1, color2, t);   //line 33 NullRef...
         if(cam.backgroundColor == Color.white)
         {
             Application.LoadLevel("TestScene1");
         }
     }
 }
 
 void OnMouseEnter()
 {
     renderer.material.color = Color.red;    
 }
 
 void OnMouseExit()
 {
     renderer.material.color = Color.white;    
 }
 
 void OnMouseDown()
 {
     newGame = true;
 }

}

I got NullReferenceException in line 33. I don't now how to continue. How can I get the camera and change the parameters?

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

3 Replies

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

Answer by Zyklon · Dec 05, 2011 at 05:30 PM

Finally, it's working! I had some problems with the Color.Lerp, but thats also working now.

The script on the camera:

 using UnityEngine;
 using System.Collections;
 
 public class CameraChangeColor : MonoBehaviour 
 {
     public bool click = false;
     
     public Color color1 = Color.black;
     public Color color2 = Color.white;
     public float duration = 3.0f;
     private float deltaTime = 0.0f;
     
     void Start () 
     {
         color1 = Color.black;
         color2 = Color.white;
         duration = 3.0f;
         deltaTime = 0.0f;
     }
     
     void Update () 
     {
         if(click)
         {
             deltaTime += Time.deltaTime;
             if(deltaTime < duration)
             {
                 camera.backgroundColor = Color.Lerp(color1, color2, deltaTime);
             }
             else
             {
                 Application.LoadLevel("TestScene1");    
             }
         }
     }
 }


... and on the menu text:

 using UnityEngine;
 using System.Collections;
 
 public class MainMenuGUINewGame : MonoBehaviour 
 {
     public GameObject go;
     public CameraChangeColor other;
     
     void Awake() 
     {
         go = GameObject.Find("Camera");
         other = (CameraChangeColor) go.GetComponent(typeof(CameraChangeColor));
     }
     
     void OnMouseEnter()
     {
         renderer.material.color = Color.red;    
     }
     
     void OnMouseExit()
     {
         renderer.material.color = Color.white;    
     }
     
     void OnMouseDown()
     {
         other.click = true;
     }
 }


It's looks nice. :D

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
avatar image
1

Answer by aldonaletto · Dec 05, 2011 at 12:55 PM

Replace the instruction in Awake:

void Awake() 
{
    cam = Camera.main;
}
I suspect Camera.mainCamera doesn't even exist! Anyway, the main camera must be tagged MainCamera (what it is by default).
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
avatar image
0

Answer by drudiverse · Jun 20, 2014 at 07:59 AM

heres a script to change the background via HSV controls

 #pragma strict
 var Hcol :float;
 var Scol :float;
 var Vcol :float;
 
 //var SPercent :float;
 //var VPercent :float;
 
 
 function Start () {
 
 // Hcol = Random.value;
 //Scol = Random.value    ;
 //Vcol = Random.value + VPercent  / 100   ;
 
 Camera.main.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
 }
 
 function Update () {
 Camera.main.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
 print (HSVtoRGB(Hcol,Scol,Vcol));
 print (HSVtoRGB(Hcol,Scol,Vcol));
 }
 
 function HSVtoRGB(h : float, s : float, v : float) {
 
 
     h=h%1;
     s=s%1;
     v=v%1;
     var r : float;
     var g : float;
     var b : float;
     var i : float;
     var f : float;
     var p : float;
     var q : float;
     var t : float; 
     i = Mathf.Floor(h * 6);
     f = h * 6 - i;
     p = v * (1 - s);
     q = v * (1 - f * s);
     t = v * (1 - (1 - f) * s);
     switch (i % 6) {
         case 0: r = v; g = t; b = p; break;
         case 1: r = q; g = v; b = p; break;
         case 2: r = p; g = v; b = t; break;
         case 3: r = p; g = q; b = v; break;
         case 4: r = t; g = p; b = v; break;
         case 5: r = v; g = p; b = q; break;
     }
     return Color(r,g,b); 
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I have the background for the main camera in a scene display the feed from a web cam/camera on a phone? 1 Answer

Setting Camera dimensions 0 Answers

Camera background is white in editor but WebGL build it is a little darker 0 Answers

2D Game: When moving the camera the background glithces 0 Answers

For some reason when i start the game my players go out of the camera zone ? or they get invisible ? 0 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