Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Hraesvelgr-tc · Aug 25, 2016 at 07:53 PM · c#buttongetcomponentaddcomponent

how can i add another scripts functionality to a button on another script?

i have 23 public vector3's

         public Vector3 vertice0 = new Vector3(-1,1,1); 
         public Vector3 vertice1 = new Vector3(1,1,1); 
         public Vector3 vertice2 = new Vector3(-1,-1,1); 
         public Vector3 vertice3 = new Vector3(1,-1,1); 
         //back face
         public Vector3 vertice4 = new Vector3(1,1,-1); 
         public Vector3 vertice5 = new Vector3(-1,1,-1); 
         public Vector3 vertice6 = new Vector3(1,-1,-1); 
         public Vector3 vertice7 = new Vector3(-1,-1,-1); 
         //left face
         public Vector3 vertice8 = new Vector3(-1,1,-1);
         public Vector3 vertice9 = new Vector3(-1,1,1);
         public Vector3 vertice10 = new Vector3(-1,-1,-1);
         public Vector3 vertice11 = new Vector3(-1,-1,1);
         //right face
         public Vector3 vertice12 = new Vector3(1,1,1);
         public Vector3 vertice13 = new Vector3(1,1,-1);
         public Vector3 vertice14 = new Vector3(1,-1,1);
         public Vector3 vertice15 = new Vector3(1,-1,-1);
         //top face
         public Vector3 vertice16 = new Vector3(-1,1,-1);
         public Vector3 vertice17 = new Vector3(1,1,-1);
         public Vector3 vertice18 = new Vector3(-1,1,1);
         public Vector3 vertice19 = new Vector3(1,1,1);
         //bottom face
         public Vector3 vertice20 = new Vector3(-1,-1,1);
         public Vector3 vertice21 = new Vector3(1,-1,1);
         public Vector3 vertice22 = new Vector3(-1,-1,-1);
         public Vector3 vertice23 = new Vector3(1,-1,-1);

i tried turning it into a separate class but my array that utilizes the vertice0, vertice1 etc.. could not acquire the vector3's. i than tried {get ; set;} on the tail end but i received an error of unexpected symbol";" for the first three public variables. so now I'm thinking that since i want to access these variables in multiple scripts that i should place them in a separate resource file. my goal is i have a gameFuntionality script which loads my UI which are gameobjects in the game to display the menus and buttons etc.. one of those buttons im trying to connect to my meshbuilder script so when it clicks it spawns the cube. atm i don't have the game objects i just have two buttons. one of those buttons was to test my lvling up the other is to spawn the cube. as you can see i have no pointers or loading of meshbuilder .cs thats becuase no mater which way i did it making a game prefab with my script and instantiating it or trying to do a call meshbuilder cubeBuild = new meshbuilder; cubeBuild.CubeMe(); or doing a resource load for the file in awake still resulted in null results some most of the time i would get "null exception reference" . nothing occurred when i clicked the button. this is the main reference ive been going back to though when i use it i dont get any errors in vim but in unity i get the null reference exception to line 69 of gameFunctionality.cs i have tried another method of removing monobehavior but than the whole script breaks.

meshbuilder.cs

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(MeshRenderer))]
 [RequireComponent(typeof(MeshFilter))]
 public class meshbuilder : MonoBehaviour
 {   
     //GameObject Cube = new GameObject();
     //public Material Red = Resources.Load("Red",typeof(Material)) as Material;
             //front face
         public Vector3 vertice0 = new Vector3(-1,1,1); //Left Top Front vertice 0                 lft  0
         public Vector3 vertice1 = new Vector3(1,1,1); //right top front vertice 1                rtf  1
         public Vector3 vertice2 = new Vector3(-1,-1,1); //left bottom front vertice 2            lbmf 2
         public Vector3 vertice3 = new Vector3(1,-1,1); //right bottom front vertice 3            rbmf 3
         //back face
         public Vector3 vertice4 = new Vector3(1,1,-1); //right top back vertice 4                rtb  4
         public Vector3 vertice5 = new Vector3(-1,1,-1); //left top back vertice 5                ltb  5
         public Vector3 vertice6 = new Vector3(1,-1,-1); //right bottom back vertice 6            rbmb 6
         public Vector3 vertice7 = new Vector3(-1,-1,-1); //left bottom back vertice 7            lbmb 7
         //left face
         public Vector3 vertice8 = new Vector3(-1,1,-1);//left top back vertice 8                 ltb 5
         public Vector3 vertice9 = new Vector3(-1,1,1);//left top front vertice 9                 lft 0
         public Vector3 vertice10 = new Vector3(-1,-1,-1);//left bottom back vertice 10           lbmb7
         public Vector3 vertice11 = new Vector3(-1,-1,1);//left bottom front vertice 11           lbmf
         //right face
         public Vector3 vertice12 = new Vector3(1,1,1);//right top front vertice 12               rtf
         public Vector3 vertice13 = new Vector3(1,1,-1);//right top back vertice 13               rtb
         public Vector3 vertice14 = new Vector3(1,-1,1);//right bottom front vertice 14           rbmf
         public Vector3 vertice15 = new Vector3(1,-1,-1);//right bottom back vertice 15           rbmb
         //top face
         public Vector3 vertice16 = new Vector3(-1,1,-1);//left top back vertice 16               ltb
         public Vector3 vertice17 = new Vector3(1,1,-1);//right top back vertice 17               rtb
         public Vector3 vertice18 = new Vector3(-1,1,1);//left top front vertice 18               lft
         public Vector3 vertice19 = new Vector3(1,1,1);//right top front vertice 19               rtf
         //bottom face
         public Vector3 vertice20 = new Vector3(-1,-1,1);//left bottom front vertice 20           lbmf
         public Vector3 vertice21 = new Vector3(1,-1,1);//right bottom front vertice 21           rbmf
         public Vector3 vertice22 = new Vector3(-1,-1,-1);//left bottom back vertice 22           lbmb
         public Vector3 vertice23 = new Vector3(1,-1,-1);//right bottom back vertice 23           rbmb
 
 
     
 
     // mesh building properties    
     public void CubeMe()
     {
         MeshFilter Filtration = GetComponent<MeshFilter>();
         Mesh CubeMorph = Filtration.mesh;
         GetComponent<MeshRenderer>().material = Resources.Load("Red",typeof(Material)) as Material;
     
         //VerticesArray
         Vector3[] VertArray = new Vector3[]
         {
             //Front  Face
             vertice0,//0
             vertice1,//1
             vertice2,//2
             vertice3,//3
             //back face
             vertice4,//4
             vertice5,//5
             vertice6,//6
             vertice7,//7
             //left face
             vertice8,//8
             vertice9,//9
             vertice10,//10
             vertice11,//11
             //right face
             vertice12,//12
             vertice13,//13
             vertice14,//14
             vertice15,//15
             //top face
             vertice16,//16
             vertice17,//17
             vertice18,//18
             vertice19,//19
             //bottom face
             vertice20,//20
             vertice21,//21
             vertice22,//22
             vertice23//23
         };
 
         //triangles array clockwise points equals visablity
         int[] triArray = new int[]
         {
             //front face
             0,2,3,
             3,1,0,
 
             //back face
             4,6,7,
             7,5,4,
 
             //left face
             8,10,11,
             11,9,8,
 
             //right face
             12,14,15,
             15,13,12,
 
             //top face
             16,18,19,
             19,17,16 ,
 
             //bottom face
             20,22,23,
             23,21,20
         };
 
         //uvs
         Vector2[] uvs = new Vector2[]
         {
             //front face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //back face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //left face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //right face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //top face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //bottom face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0)
         };
         CubeMorph.Clear();
         CubeMorph.vertices = VertArray;
         CubeMorph.triangles = triArray;
         CubeMorph.uv = uvs;
         CubeMorph.Optimize();
         CubeMorph.RecalculateNormals();
 
     }
     void awake ()
     {
         CubeMe ();
     }
      void start()
     {
         awake ();
     }
 
 
     
     // Update is called once per frame
     void Update () 
     {
         start ();
     }

gameFunctionality.cs

     using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime;
 using System;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 [RequireComponent()]
 public class gameFunctionality : MonoBehaviour 
 {
     public float Xp = 0f; // experience points used in lvl up, enemy and player scripts, items
     public float XpToNxtLvl = 0f; // player and item equation to determin amount of points required for lvling up
     public float Health = 100f; // a stat for the player
     public float StatPoints = 0f; // number of points to allocate into stats for items and player
     public float Level = 1f; // current lvl of item or character
     public Texture2D button; // texture of a button
 
     //onclick event for button
     public void OnMouseDown()
     {
         Xp += 1000000000;
         print("+10Xp");
     }
     //onclickRelease event for button
     public void OnMouseUp()
     {
         XpGain ();
         if(Xp >= XpToNxtLvl)
         {
             print("Level Up!");
         }
     }
     //function to acquire meshbuilder script
     void build ()
     {         
 
 
     }
     //  not sure if this is needed but possible use in future probaly could just combine it with nxtLvl function
     void XpGain()
     {
         if( Xp >= XpToNxtLvl)
         {
             NextLvl();
         }
     }
     //acquiring xp
     void XpToLvl()
     {
         XpToNxtLvl = ((1+Mathf.Pow(Mathf.Sqrt(5f),Level)) - (1f-(Mathf.Pow(Mathf.Sqrt(5f),Level)))) /(Mathf.Pow(2,Level)*Mathf.Sqrt(5f));
     }
     //  LevelUP!
     void NextLvl()
     {
         Xp -= Xp;
         Health += 50;
         StatPoints += 10;
         Level += 1;
     }
     //  Button and other user interface things
     void OnGUI()
     {  
             if(GUI.Button(new Rect(0,0,100,20),"Cube Me!"))
             {
                 CubeMe ();
             }
 
         if (GUI.Button (new Rect (10, 70, 100, 20), "Click")) 
         {
             OnMouseDown ();
             OnMouseUp ();
         }
     }
     //intializes before start
     public void awake()
     {
 
     }
     // Use this for initialization    
     void Start () 
     {    
 
     }
     // Update is called once per frame
     void Update () 
     {
         XpToLvl();
     }
 }


Comment
Add comment · Show 2
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 Hraesvelgr-tc · Aug 25, 2016 at 10:46 PM 0
Share

i just tried turning the arrays into public static variables as well as the public vertices. make Cube$$anonymous$$e(); just acquiring the mesh and mesh filter and material calling on the arrays etc. but i still get the null object reference. Edit: so when i made Public Cube$$anonymous$$e(){} into a static variable i got two errors first was on gameFunctionality.cs line 70 cubeBuild.Cube$$anonymous$$e(); cs0176; getting rid of the line meshbuilder cubeBuild = getcomponent(); and changing public Cube$$anonymous$$e() into static and line 70 to meshbuilder.Cube$$anonymous$$e() got ride of error cs0176. but that error gets resolved i get the second error in meshbuilder.cs by making Cube$$anonymous$$e() public static my line GetComponent ().material = Resources.Load ("Red", typeof($$anonymous$$aterial)) as $$anonymous$$aterial; errors into i need an object reference i tried doing Cubemorph.GetComponent... , public $$anonymous$$aterial Red = GetComponent.... but what ever i reference i get logical errors like Cube$$anonymous$$orph is a mesh doesnt contain definition GetComponent.

avatar image Hraesvelgr-tc Hraesvelgr-tc · Aug 25, 2016 at 11:57 PM 0
Share

so a new reply ins$$anonymous$$d of an edit for ease of reading. getting rid of the static keyword on Cube$$anonymous$$e() in meshbuilder.cs. removing the code from the first ongui button if statement so it looks like

void OnGUI() {

         if(GUI.Button(new Rect(0,0,100,20),"Cube $$anonymous$$e!"))
         {
 
             build ();
         }
 
         if (GUI.Button (new Rect (10, 70, 100, 20), "Click")) 
         {
             On$$anonymous$$ouseDown ();
             On$$anonymous$$ouseUp ();
         }
     }

the build function i added this

void build () {
Resources.Load("meshbuilder",typeof(meshbuilder)); gameObject.AddComponent (); //meshbuilder.VertArray; //meshbuilder.triArray; //meshbuilder.uvs; } so now i c it loads in the inspector on play but the cube is not appearing on click but ins$$anonymous$$d the file does load. im not sure if the resources.load is needed but i added it just incase

2 Replies

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

Answer by Hraesvelgr-tc · Aug 26, 2016 at 12:16 AM

ok i figured it out gameFunctionality.cs

voidBuild() is turned to this

 void build ()
     {        
         
         meshbuilder cubebuild = GetComponent <meshbuilder>()as meshbuilder;
         cubebuild.CubeMe ();
 }

gameObject.AddComponent < meshbuilder > (); was added to the start function now im not sure if doing it this way i need static variables in meshbuilder.cs still but i kept it the way it is

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 juicyz · Aug 26, 2016 at 04:47 AM

From quick look, it is hard to tell what you want. You should rephrase the paragraph and get rid of the unneeded information. Just include what you have, what you want to happen, and the errors that occurred. As I said, it's hard to tell what isn't working but I'm guessing it's this section of the code:

  void OnGUI()
  {  
          if(GUI.Button(new Rect(0,0,100,20),"Cube Me!"))
          {
              CubeMe ();
          }
  
      if (GUI.Button (new Rect (10, 70, 100, 20), "Click")) 
      {
          OnMouseDown ();
          OnMouseUp ();
      }
  }

CubeMe(); won't work because you need to put |meshbuilder|.CubeMe(); it needs a reference to the space you are trying to access. You need to put in the gameFunctionality class, a 'meshbuilder = new meshbuilder();' in either local space to the CubeMe function or in private space to that class.

If your meshbuilder is in a seperate game object than the gameFunctionality, then you need to do as that reference said, use the GetComponent function.

To fully help you out, I'll ask a couple of questions to declare the scope of the problem: Is your meshbuilder script on a seperate object than your gameFunctionality script or where are they at? Post the exact error, word for word please.

Also you can't get in errors in vim because it's just a text editor, it has no compiling operations and it can't run your code.

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

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

Multiple Cars not working 1 Answer

Cache GetComponent correctly in C# 2 Answers

Distribute terrain in zones 3 Answers

How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers

GetComponent and AddComponent with variables for Collider type 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