Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How can i set default setting

Discussion in 'Scripting' started by yasinbozatr, Jul 19, 2018.

  1. yasinbozatr

    yasinbozatr

    Joined:
    Jul 19, 2018
    Posts:
    6
    hi guys i have have a problem. My problem about prefab. i created prefab but how can i set default script settings.
     
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Do you mean how do you assign variables by default? That's done simply like this:

    Code (CSharp):
    1.  
    2. public float myFloat = 5.0f;
    3. public bool myBool = true;
    4. public int myInt = 6;
    5.  
    Make your question a bit more clearer please ;)
     
  3. yasinbozatr

    yasinbozatr

    Joined:
    Jul 19, 2018
    Posts:
    6
    I made a hand-held object. when I convert this object to prefab, the camera option I assign to the script becomes empty automatically
     
  4. yasinbozatr

    yasinbozatr

    Joined:
    Jul 19, 2018
    Posts:
    6
    I want to make sure that the camera object I set for the camera option of the object I'm translating is still the same when I rotate the object, but it does not. I created a prefab file when I press the key that I assigned with the script, but I can not hold the prefab file that it created because the prefabricated camera and character object could not be assigned automatically
     
  5. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Okay, this is because the Camera is not part of the prefab. The best way to fix this in my opinion is to attach a script to the camera, with something like this in it:

    Code (CSharp):
    1.  
    2. public class CamSingleton : MonoBehaviour {
    3.     public static Camera cam;
    4.  
    5.     void Awake () {
    6.         cam = GetComponent<Camera>();
    7.     }
    8. }

    Then, in the script on your prefab, you assign your camera variable to CamSingleton.cam.

    Let me know if any of that didn't make sense.
     
  6. yasinbozatr

    yasinbozatr

    Joined:
    Jul 19, 2018
    Posts:
    6
    Thank you so much i will try.
     
  7. yasinbozatr

    yasinbozatr

    Joined:
    Jul 19, 2018
    Posts:
    6
    I used the camera object as a transform. I can not quite understand how to make it like you said.
     
  8. yasinbozatr

    yasinbozatr

    Joined:
    Jul 19, 2018
    Posts:
    6
    before prefab 2.png



    it after create prefab.
    1.png

    I need to transform the camera object for move the object
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Fields on prefabs can only refer to other objects/components of that prefab, or refer to another prefab. They can't refer to other objects existing in a scene (because who knows if you're even going to instantiate that prefab in that scene). So to get references to those you have to do that at runtime.

    You can set a reference on the instantiated prefab as soon as you instantiate from the script that did so, or you can have the prefab itself find references as soon as it is instantiated, such as in an attached script's Awake or Start method.

    For example, you can get a reference to the first camera in your scene tagged MainCamera with Camera.main, or its transform with Camera.main.transform I believe. As the below docs mention, you should cache the result instead of calling Camera.main repeatedly.

    https://docs.unity3d.com/ScriptReference/Camera-main.html

    You could also just find the GameObject with the attached camera component with GameObject.FindWithTag("MainCamera"). But again cache the result.